Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not insert initial value when input is empty in editor #8773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/app/src/components/PageEditor/PageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
// set to ref
initialValueRef.current = initialValue;
}, [initialValue]);
const [markdownToPreview, setMarkdownToPreview] = useState<string>(initialValue);

const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);

const [markdownToPreview, setMarkdownToPreview] = useState<string>(codeMirrorEditor?.getDoc() ?? '');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • markdownToPreview の初期値にエディターで入力中の文字列を入れるようにした
  • ドラフトが空文字列の時に CodeMirrorEditorMain に渡す onChange がトリガーされたかったためこのような改修を行なった

const setMarkdownPreviewWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string) => {
setMarkdownToPreview(value);
})), []);
Expand All @@ -159,8 +162,6 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
}, [setMarkdownPreviewWithDebounce]);


const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);

const { scrollEditorHandler, scrollPreviewHandler } = useScrollSync(GlobalCodeMirrorEditorKey.MAIN, previewRef);

const scrollEditorHandlerThrottle = useMemo(() => throttle(25, scrollEditorHandler), [scrollEditorHandler]);
Expand Down
7 changes: 3 additions & 4 deletions apps/app/src/server/service/yjs-connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ class YjsConnectionManager {

await this.mdb.flushDocument(pageId);

const persistedCodeMirrorText = persistedYdoc.getText('codemirror').toString();
const currentCodeMirrorText = currentYdoc.getText('codemirror').toString();

if (persistedCodeMirrorText === '' && currentCodeMirrorText === '') {
// If no write operation has been performed, insert initial value
const clientsSize = currentYdoc.store.clients.size;
if (clientsSize === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ydoc の初期化後に何らかの操作を行うことにより store.clients.size が増えていく

参考

https://speakerdeck.com/kentomoriwaki/wakatutaqi-ninareru-crdt-woshi-tutagong-tong-bian-ji?slide=22

currentYdoc.getText('codemirror').insert(0, initialValue);
}

Expand Down
Loading