Skip to content

Commit

Permalink
feat: feat add debounce editor
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 24, 2023
1 parent 303e01b commit 3697f5e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"react-dom": "18.2.0",
"react-i18next": "^13.5.0",
"scroll-into-view-if-needed": "^3.1.0",
"tippy.js": "^6.3.7"
"tippy.js": "^6.3.7",
"use-debounce": "^10.0.0"
},
"engines": {
"node": ">=18"
Expand Down
20 changes: 14 additions & 6 deletions src/components/editor/live-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect, useState } from 'react'

import { EditorContent, useEditor } from '@tiptap/react'
import { Color } from '@tiptap/extension-color'
Expand All @@ -7,6 +7,7 @@ import TextStyle from '@tiptap/extension-text-style'
import StarterKit from '@tiptap/starter-kit'

import MarkdownIt from 'markdown-it'
import { useDebounce } from 'use-debounce';

import { MenuBar } from './menu-bar'
import { MenuBubble } from './intelli/menu/menu-bubble'
Expand All @@ -16,7 +17,6 @@ import { createAiBlock } from './intelli/ai-block-extension'
import TrackChangeExtension from './diff/track-change-extension'
import { Sidebar } from './sidebar'
import { CharacterCount } from "@tiptap/extension-character-count";
import { abs } from "lib0/math";

const md = new MarkdownIt()

Expand Down Expand Up @@ -54,7 +54,7 @@ const extensions = [
TextStyle.configure({ types: [ListItem.name] }),
]

const content = `
const placeHolder = `
# 3B editor
Hi there, 3B is editor for Unit Mesh architecture paradigms, the next-gen software architecture.
Expand Down Expand Up @@ -83,14 +83,22 @@ Testing grammar and spellings, select long text to see the menu.
const LiveEditor = () => {
const editor = useEditor({
extensions,
content: md.render(content),
content: md.render(placeHolder),
editorProps: {
attributes: {
class: 'prose lg:prose-xl bb-editor-inner',
},
},
}
})

const [debouncedEditor] = useDebounce(editor?.state.doc.content, 2000);
useEffect(() => {
if (debouncedEditor) {
// save
console.info('save', debouncedEditor)
}
}, [debouncedEditor]);

return (<div className={'w-full'}>
<div className={'editor-block'}>
<div className={'domain-buttons'}>
Expand All @@ -106,7 +114,7 @@ const LiveEditor = () => {
{editor && <MenuBubble editor={editor}/>}

{editor && <div className="character-count">
<span>{abs(editor.state.selection.$from.pos - editor.state.selection.$to.pos)} selected</span>
<span>{Math.abs(editor.state.selection.$from.pos - editor.state.selection.$to.pos)} selected</span>
&nbsp;&nbsp;
<span>{editor.storage.characterCount.characters()} characters</span>
&nbsp;&nbsp;
Expand Down

0 comments on commit 3697f5e

Please sign in to comment.