|
|
@@ -4,73 +4,69 @@ import { Compartment, StateEffect } from '@codemirror/state';
|
|
|
import { EditorView } from '@codemirror/view';
|
|
|
import type { ViewUpdate } from '@codemirror/view';
|
|
|
|
|
|
-export type SetCaretLine = (lineNumber?: number) => void;
|
|
|
+export type SetCaretLine = (lineNumber?: number, schedule?: boolean) => void;
|
|
|
|
|
|
const setCaretLine = (view?: EditorView, lineNumber?: number): void => {
|
|
|
const doc = view?.state.doc;
|
|
|
|
|
|
if (doc == null || view == null) {
|
|
|
- throw RangeError;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- const posOfLineEnd = doc.line(lineNumber ?? 1).to;
|
|
|
- view?.dispatch({
|
|
|
- selection: {
|
|
|
- anchor: posOfLineEnd,
|
|
|
- head: posOfLineEnd,
|
|
|
- },
|
|
|
- scrollIntoView: true,
|
|
|
- effects: EditorView.scrollIntoView(posOfLineEnd, {
|
|
|
- x: 'start',
|
|
|
- y: 'start',
|
|
|
- }),
|
|
|
- });
|
|
|
- // focus
|
|
|
- view?.focus();
|
|
|
+ try {
|
|
|
+ const posOfLineEnd = doc.line(lineNumber ?? 1).to;
|
|
|
+ view?.dispatch({
|
|
|
+ selection: {
|
|
|
+ anchor: posOfLineEnd,
|
|
|
+ head: posOfLineEnd,
|
|
|
+ },
|
|
|
+ scrollIntoView: true,
|
|
|
+ });
|
|
|
+ // focus
|
|
|
+ view?.focus();
|
|
|
+ }
|
|
|
+ catch (_: unknown) {
|
|
|
+ // if posOfLineEnd is not found.
|
|
|
+ }
|
|
|
|
|
|
};
|
|
|
|
|
|
-export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
|
|
|
-
|
|
|
- return useCallback((lineNumber) => {
|
|
|
+const setCaretLineSchedule = (view?: EditorView, lineNumber?: number): void => {
|
|
|
|
|
|
- try {
|
|
|
- setCaretLine(view, lineNumber);
|
|
|
- }
|
|
|
- catch (_: unknown) {
|
|
|
- //
|
|
|
- }
|
|
|
+ // support Yjs lazy load doc
|
|
|
+ const compartment = new Compartment();
|
|
|
|
|
|
- console.log('Oooops');
|
|
|
+ const initDocListenerExtension = EditorView.updateListener.of((v: ViewUpdate) => {
|
|
|
|
|
|
- // support Yjs lazy load doc
|
|
|
- const compartment = new Compartment();
|
|
|
+ // TODO: use ySyncAnnotation for if statement and remove "currentPageYjsData?.hasRevisionBodyDiff === false" in Header.tsx
|
|
|
+ // Ref: https://github.com/yjs/y-codemirror.next/pull/30
|
|
|
+ if (v.docChanged && v.changes.desc.length === 0) {
|
|
|
|
|
|
- const initDocListenerExtension = EditorView.updateListener.of((v: ViewUpdate) => {
|
|
|
+ setCaretLine(view, lineNumber);
|
|
|
|
|
|
- console.log(v);
|
|
|
- // TODO: change conditional statement
|
|
|
- if (v.docChanged && v.changes.desc.length === 0) {
|
|
|
+ view?.dispatch({
|
|
|
+ effects: compartment.reconfigure([]),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- try {
|
|
|
- setCaretLine(view, lineNumber);
|
|
|
- }
|
|
|
- catch (_: unknown) {
|
|
|
- // if posOfLineEnd is not found.
|
|
|
- }
|
|
|
+ view?.dispatch({
|
|
|
+ effects: StateEffect.appendConfig.of(
|
|
|
+ compartment.of(initDocListenerExtension),
|
|
|
+ ),
|
|
|
+ });
|
|
|
+};
|
|
|
|
|
|
- view?.dispatch({
|
|
|
- effects: compartment.reconfigure([]),
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
+export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
|
|
|
|
|
|
- view?.dispatch({
|
|
|
- effects: StateEffect.appendConfig.of(
|
|
|
- compartment.of(initDocListenerExtension),
|
|
|
- ),
|
|
|
- });
|
|
|
+ return useCallback((lineNumber?: number, schedule?: boolean) => {
|
|
|
|
|
|
+ if (schedule) {
|
|
|
+ setCaretLineSchedule(view, lineNumber);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ setCaretLine(view, lineNumber);
|
|
|
+ }
|
|
|
|
|
|
}, [view]);
|
|
|
|