Skip to content

Commit

Permalink
Feat: fix readonly value change
Browse files Browse the repository at this point in the history
  • Loading branch information
tiavina-mika committed Aug 15, 2024
1 parent 47b94af commit 5bba5b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/components/TextEditorReadOnly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ import { useTextEditor } from "../hooks/useTextEditor";
type TextEditorProps = {
className?: string;
value?: string;
tab?: 'editor' | 'preview';
};

const TextEditorReadOnly = ({
value,
className,
tab = 'preview',
...editorOptions
}: TextEditorProps) => {
const editor = useTextEditor({
value,
tab,
tab: 'preview',
editable: false,
...editorOptions
})
Expand Down
20 changes: 19 additions & 1 deletion src/hooks/useTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,32 @@ export const useTextEditor = ({
...editorOptions,
});

// set initial value for edition even if it's already set (below)
/**
* EDITABLE = true
* this updates the editor content on every value change
* set initial value for edition even if it's already set (below)
*/
useEffect(() => {
if (!editor) return;
// if (editor.isDestroyed) return;
if (!(value && editor.isEmpty)) return;

editor.commands.setContent(value);
}, [editor, value]);

/**
* EDITABLE = false
* this updates the editor content on every value change for readonly mode
* it's useful for content that changed externally
* @example tab change
*/
useEffect(() => {
if (!editor) return;
if (value && !editable && tab === 'preview') {
editor.commands.setContent(value);
}
}, [editor, value]);


// useEffect(() => {
// editor?.off("selectionUpdate");
Expand Down

0 comments on commit 5bba5b5

Please sign in to comment.