@dxos/react-ui-editor 0.3.11-main.008b7c6 → 0.3.11-main.08776d7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +745 -810
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +12 -5
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +16 -19
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +6 -8
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -11
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +26 -17
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/{util → extensions}/cursor.d.ts +10 -2
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/image.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +3 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/extensions/sync.d.ts +7 -0
- package/dist/types/src/extensions/sync.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts +5 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge.d.ts +2 -1
- package/dist/types/src/hooks/automerge.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +28 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +2 -32
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/yjs.d.ts +2 -1
- package/dist/types/src/hooks/yjs.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +1 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +25 -24
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +45 -41
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +101 -89
- package/src/components/TextEditor/comments.stories.tsx +362 -0
- package/src/extensions/autocomplete.ts +18 -17
- package/src/extensions/automerge/automerge.ts +15 -46
- package/src/extensions/automerge/cursor.ts +2 -1
- package/src/extensions/automerge/defs.ts +13 -14
- package/src/extensions/automerge/semaphore.ts +21 -16
- package/src/extensions/awareness.ts +27 -33
- package/src/extensions/code.ts +28 -23
- package/src/extensions/comments.ts +234 -236
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/hr.ts +10 -12
- package/src/extensions/image.ts +40 -22
- package/src/extensions/index.ts +3 -1
- package/src/extensions/link.ts +86 -88
- package/src/extensions/markdown/highlight.ts +3 -12
- package/src/extensions/outliner.ts +12 -0
- package/src/extensions/sync.ts +15 -0
- package/src/extensions/table.ts +10 -4
- package/src/extensions/tasklist.ts +54 -79
- package/src/extensions/yjs/cursor.ts +2 -1
- package/src/extensions/yjs/yjs.test.ts +1 -1
- package/src/extensions/yjs/yjs.ts +2 -2
- package/src/hooks/automerge.ts +2 -1
- package/src/hooks/defs.ts +47 -0
- package/src/hooks/index.ts +3 -1
- package/src/hooks/useTextModel.ts +3 -49
- package/src/hooks/yjs.ts +2 -23
- package/src/index.ts +3 -0
- package/src/styles/markdown.ts +0 -2
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +51 -78
- package/dist/types/src/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/util/cursor.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -3
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -11
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- /package/dist/types/src/{util/util.d.ts → util.d.ts} +0 -0
- /package/src/{util/util.ts → util.ts} +0 -0
|
@@ -2,58 +2,39 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
WidgetType,
|
|
9
|
-
MatchDecorator,
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type DecorationSet,
|
|
12
|
-
type ViewUpdate,
|
|
13
|
-
} from '@codemirror/view';
|
|
14
|
-
|
|
15
|
-
// TODO(burdon): Reconcile with theme.
|
|
16
|
-
const styles = EditorView.baseTheme({
|
|
17
|
-
'& .cm-task-item': {
|
|
18
|
-
paddingLeft: '4px',
|
|
19
|
-
paddingRight: '8px',
|
|
20
|
-
},
|
|
21
|
-
});
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
|
|
22
8
|
|
|
23
9
|
class CheckboxWidget extends WidgetType {
|
|
24
|
-
constructor(
|
|
25
|
-
private readonly _pos: number,
|
|
26
|
-
private _checked: boolean,
|
|
27
|
-
private readonly _indent: number,
|
|
28
|
-
private readonly _onCheck?: (check: boolean) => void,
|
|
29
|
-
) {
|
|
10
|
+
constructor(private _checked: boolean) {
|
|
30
11
|
super();
|
|
31
12
|
}
|
|
32
13
|
|
|
33
|
-
override eq(other:
|
|
34
|
-
return this.
|
|
14
|
+
override eq(other: this) {
|
|
15
|
+
return this._checked === other._checked;
|
|
35
16
|
}
|
|
36
17
|
|
|
37
|
-
toDOM(view: EditorView) {
|
|
38
|
-
const
|
|
39
|
-
wrap.className = 'cm-task-item';
|
|
40
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
41
|
-
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
42
|
-
|
|
43
|
-
const input = wrap.appendChild(document.createElement('input'));
|
|
18
|
+
override toDOM(view: EditorView) {
|
|
19
|
+
const input = document.createElement('input');
|
|
44
20
|
input.type = 'checkbox';
|
|
45
21
|
input.checked = this._checked;
|
|
46
|
-
if (
|
|
22
|
+
if (view.state.readOnly) {
|
|
47
23
|
input.setAttribute('disabled', 'true');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
24
|
+
} else {
|
|
25
|
+
input.onmousedown = (event: Event) => {
|
|
26
|
+
const pos = view.posAtDOM(input);
|
|
27
|
+
const text = view.state.sliceDoc(pos, pos + 3);
|
|
28
|
+
if (text === (this._checked ? '[x]' : '[ ]')) {
|
|
29
|
+
view.dispatch({
|
|
30
|
+
changes: { from: pos + 1, to: pos + 2, insert: this._checked ? ' ' : 'x' },
|
|
31
|
+
});
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
}
|
|
53
34
|
};
|
|
54
35
|
}
|
|
55
36
|
|
|
56
|
-
return
|
|
37
|
+
return input;
|
|
57
38
|
}
|
|
58
39
|
|
|
59
40
|
override ignoreEvent() {
|
|
@@ -61,61 +42,55 @@ class CheckboxWidget extends WidgetType {
|
|
|
61
42
|
}
|
|
62
43
|
}
|
|
63
44
|
|
|
45
|
+
const checkedDeco = Decoration.replace({ widget: new CheckboxWidget(true) });
|
|
46
|
+
const uncheckedDeco = Decoration.replace({ widget: new CheckboxWidget(false) });
|
|
47
|
+
|
|
64
48
|
export type TasklistOptions = {};
|
|
65
49
|
|
|
66
50
|
export const tasklist = (options: TasklistOptions = {}) => {
|
|
67
|
-
|
|
68
|
-
// Allows for indents to be greater than AST would allow.
|
|
69
|
-
const taskMatcher = new MatchDecorator({
|
|
70
|
-
regexp: /^(\s*)- \[([ xX])\]\s/g,
|
|
71
|
-
decoration: (match, view, pos) => {
|
|
72
|
-
const indent = Math.floor(match[1].length / 2);
|
|
73
|
-
const checked = match[2] === 'x' || match[2] === 'X';
|
|
74
|
-
return Decoration.replace({
|
|
75
|
-
widget: new CheckboxWidget(
|
|
76
|
-
pos,
|
|
77
|
-
checked,
|
|
78
|
-
indent,
|
|
79
|
-
view.state.readOnly
|
|
80
|
-
? undefined
|
|
81
|
-
: (checked) => {
|
|
82
|
-
const idx = pos + match[0].indexOf('[') + 1;
|
|
83
|
-
view.dispatch({
|
|
84
|
-
changes: {
|
|
85
|
-
from: idx,
|
|
86
|
-
to: idx + 1,
|
|
87
|
-
insert: checked ? 'x' : ' ',
|
|
88
|
-
},
|
|
89
|
-
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
90
|
-
selection: {
|
|
91
|
-
anchor: idx + 3,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
),
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const tasks = ViewPlugin.fromClass(
|
|
51
|
+
return ViewPlugin.fromClass(
|
|
101
52
|
class {
|
|
102
|
-
|
|
53
|
+
decorations: DecorationSet;
|
|
54
|
+
|
|
103
55
|
constructor(view: EditorView) {
|
|
104
|
-
this.
|
|
56
|
+
this.decorations = buildDecorations(view);
|
|
105
57
|
}
|
|
106
58
|
|
|
107
59
|
update(update: ViewUpdate) {
|
|
108
|
-
|
|
60
|
+
if (update.docChanged || update.viewportChanged || update.selectionSet) {
|
|
61
|
+
this.decorations = buildDecorations(update.view);
|
|
62
|
+
}
|
|
109
63
|
}
|
|
110
64
|
},
|
|
111
65
|
{
|
|
112
|
-
decorations: (
|
|
66
|
+
decorations: (v) => v.decorations,
|
|
67
|
+
// TODO(burdon): Is this still required?
|
|
113
68
|
provide: (plugin) =>
|
|
114
69
|
EditorView.atomicRanges.of((view) => {
|
|
115
|
-
return view.plugin(plugin)?.
|
|
70
|
+
return view.plugin(plugin)?.decorations || Decoration.none;
|
|
116
71
|
}),
|
|
117
72
|
},
|
|
118
73
|
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
77
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
78
|
+
const { state } = view;
|
|
79
|
+
const cursor = state.selection.main.head;
|
|
80
|
+
|
|
81
|
+
for (const { from, to } of view.visibleRanges) {
|
|
82
|
+
syntaxTree(state).iterate({
|
|
83
|
+
enter: (node) => {
|
|
84
|
+
// Check if cursor is inside text.
|
|
85
|
+
if (node.name === 'TaskMarker' && (cursor < node.from || cursor > node.to)) {
|
|
86
|
+
const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
|
|
87
|
+
builder.add(node.from, node.to, checked ? checkedDeco : uncheckedDeco);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
from,
|
|
91
|
+
to,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
119
94
|
|
|
120
|
-
return
|
|
95
|
+
return builder.finish();
|
|
121
96
|
};
|
|
@@ -6,12 +6,13 @@ import * as Y from 'yjs';
|
|
|
6
6
|
|
|
7
7
|
import { arrayToString, stringToArray } from '@dxos/util';
|
|
8
8
|
|
|
9
|
-
import { type CursorConverter } from '
|
|
9
|
+
import { type CursorConverter } from '../cursor';
|
|
10
10
|
|
|
11
11
|
export const cursorConverter = (text: Y.Text): CursorConverter => ({
|
|
12
12
|
toCursor: (index, assoc) => {
|
|
13
13
|
return arrayToString(Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text, index, assoc)));
|
|
14
14
|
},
|
|
15
|
+
|
|
15
16
|
fromCursor: (cursor) => {
|
|
16
17
|
return (
|
|
17
18
|
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
@@ -12,7 +12,7 @@ import { type YText } from '@dxos/text-model';
|
|
|
12
12
|
describe('YJS', () => {
|
|
13
13
|
// https://docs.yjs.dev/api/shared-types/y.text
|
|
14
14
|
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
15
|
-
test('absolute position', () => {
|
|
15
|
+
test.skip('absolute position', () => {
|
|
16
16
|
const obj = new TextObject('hello world');
|
|
17
17
|
const index = obj.text.indexOf('world');
|
|
18
18
|
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
@@ -8,9 +8,9 @@ import type * as YP from 'y-protocols/awareness';
|
|
|
8
8
|
import type { YText } from '@dxos/text-model';
|
|
9
9
|
|
|
10
10
|
import { cursorConverter } from './cursor';
|
|
11
|
-
import {
|
|
11
|
+
import { Cursor } from '../cursor';
|
|
12
12
|
|
|
13
13
|
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
-
|
|
14
|
+
Cursor.converter.of(cursorConverter(content)),
|
|
15
15
|
yCollab(content, awareness),
|
|
16
16
|
];
|
package/src/hooks/automerge.ts
CHANGED
|
@@ -8,7 +8,8 @@ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
|
8
8
|
import { isNotNullOrUndefined } from '@dxos/util';
|
|
9
9
|
|
|
10
10
|
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
11
|
-
import { type EditorModel, modelState
|
|
11
|
+
import { type EditorModel, modelState } from './defs';
|
|
12
|
+
import { type UseTextModelProps } from './useTextModel';
|
|
12
13
|
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
13
14
|
import { cursorColor } from '../styles';
|
|
14
15
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension, StateField } from '@codemirror/state';
|
|
6
|
+
import type * as YP from 'y-protocols/awareness';
|
|
7
|
+
|
|
8
|
+
import { type DocAccessor } from '@dxos/echo-schema';
|
|
9
|
+
import type { YText, YXmlFragment } from '@dxos/text-model';
|
|
10
|
+
|
|
11
|
+
// Runtime data structure.
|
|
12
|
+
export type Range = {
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Persistent data structure.
|
|
18
|
+
export type Comment = {
|
|
19
|
+
id: string;
|
|
20
|
+
cursor?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type EditorModel = {
|
|
24
|
+
id: string;
|
|
25
|
+
text: () => string;
|
|
26
|
+
extension?: Extension;
|
|
27
|
+
|
|
28
|
+
// TODO(burdon): Remove.
|
|
29
|
+
content: string | YText | YXmlFragment | DocAccessor;
|
|
30
|
+
|
|
31
|
+
// TODO(burdon): Move into extension.
|
|
32
|
+
awareness?: YP.Awareness;
|
|
33
|
+
|
|
34
|
+
// TODO(burdon): Remove.
|
|
35
|
+
peer?: {
|
|
36
|
+
id: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* State field makes the model available to other extensions.
|
|
43
|
+
*/
|
|
44
|
+
export const modelState = StateField.define<EditorModel | undefined>({
|
|
45
|
+
create: () => undefined,
|
|
46
|
+
update: (model) => model,
|
|
47
|
+
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -2,59 +2,15 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { StateField, type Extension } from '@codemirror/state';
|
|
6
5
|
import { useEffect, useState } from 'react';
|
|
7
|
-
import type * as YP from 'y-protocols/awareness';
|
|
8
6
|
|
|
9
|
-
import { isAutomergeObject, type
|
|
7
|
+
import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
|
|
10
8
|
import { type Identity } from '@dxos/react-client/halo';
|
|
11
|
-
import type { YText, YXmlFragment } from '@dxos/text-model';
|
|
12
9
|
|
|
13
10
|
import { createAutomergeModel } from './automerge';
|
|
11
|
+
import { type EditorModel } from './defs';
|
|
14
12
|
import { createYjsModel } from './yjs';
|
|
15
13
|
|
|
16
|
-
// TODO(burdon): Factor out defs.
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* State field makes the model available to other extensions.
|
|
20
|
-
*/
|
|
21
|
-
export const modelState = StateField.define<EditorModel | undefined>({
|
|
22
|
-
create: () => undefined,
|
|
23
|
-
update: (model) => model,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
export type Range = {
|
|
27
|
-
from: number;
|
|
28
|
-
to: number;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type CommentRange = {
|
|
32
|
-
id: string;
|
|
33
|
-
cursor: string;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
37
|
-
export type EditorModel = {
|
|
38
|
-
id: string;
|
|
39
|
-
text: () => string;
|
|
40
|
-
extension?: Extension;
|
|
41
|
-
|
|
42
|
-
// TODO(burdon): Remove.
|
|
43
|
-
content: string | YText | YXmlFragment | DocAccessor;
|
|
44
|
-
/**
|
|
45
|
-
* @deprecated Use CursorConverter.
|
|
46
|
-
*/
|
|
47
|
-
getCursorFromRange?: (value: Range) => string;
|
|
48
|
-
getRangeFromCursor?: (cursor: string) => Range | undefined;
|
|
49
|
-
// TODO(burdon): Move into extension.
|
|
50
|
-
awareness?: YP.Awareness;
|
|
51
|
-
// TODO(burdon): Remove.
|
|
52
|
-
peer?: {
|
|
53
|
-
id: string;
|
|
54
|
-
name?: string;
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
14
|
// TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
|
|
59
15
|
export type UseTextModelProps = {
|
|
60
16
|
identity?: Identity | null;
|
|
@@ -62,11 +18,9 @@ export type UseTextModelProps = {
|
|
|
62
18
|
text?: TextObject;
|
|
63
19
|
};
|
|
64
20
|
|
|
65
|
-
// TODO(burdon): Remove YJS/Automerge deps (from UI component -- create abstraction; incl. all ECHO/Space deps).
|
|
66
|
-
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
67
21
|
export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
|
|
68
22
|
const { identity, space, text } = props;
|
|
69
|
-
const [model, setModel] = useState<EditorModel | undefined>(
|
|
23
|
+
const [model, setModel] = useState<EditorModel | undefined>();
|
|
70
24
|
useEffect(() => setModel(createModel(props)), [identity, space, text]);
|
|
71
25
|
return model;
|
|
72
26
|
};
|
package/src/hooks/yjs.ts
CHANGED
|
@@ -6,7 +6,6 @@ import * as decoding from 'lib0/decoding';
|
|
|
6
6
|
import * as encoding from 'lib0/encoding';
|
|
7
7
|
import { Observable } from 'lib0/observable';
|
|
8
8
|
import * as YP from 'y-protocols/awareness';
|
|
9
|
-
import * as Y from 'yjs';
|
|
10
9
|
import { type Doc } from 'yjs';
|
|
11
10
|
|
|
12
11
|
import { invariant } from '@dxos/invariant';
|
|
@@ -14,9 +13,9 @@ import { log } from '@dxos/log';
|
|
|
14
13
|
import { type Space } from '@dxos/react-client/echo';
|
|
15
14
|
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
16
15
|
import type { YText } from '@dxos/text-model';
|
|
17
|
-
import { arrayToString, stringToArray } from '@dxos/util';
|
|
18
16
|
|
|
19
|
-
import { type EditorModel, modelState
|
|
17
|
+
import { type EditorModel, modelState } from './defs';
|
|
18
|
+
import { type UseTextModelProps } from './useTextModel';
|
|
20
19
|
import { yjs } from '../extensions';
|
|
21
20
|
|
|
22
21
|
export const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
|
|
@@ -29,26 +28,6 @@ export const createYjsModel = ({ identity, space, text }: UseTextModelProps): Ed
|
|
|
29
28
|
id: text.doc.guid,
|
|
30
29
|
content: text.content,
|
|
31
30
|
text: () => text.content!.toString(),
|
|
32
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
33
|
-
// TODO(dmaretskyi): Refactor as cursor-converter.
|
|
34
|
-
getCursorFromRange: (range: Range) => {
|
|
35
|
-
const from = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.from));
|
|
36
|
-
const to = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.to, -1));
|
|
37
|
-
return [arrayToString(from), arrayToString(to)].join(':');
|
|
38
|
-
},
|
|
39
|
-
getRangeFromCursor: (cursor: string) => {
|
|
40
|
-
invariant(text.doc);
|
|
41
|
-
const parts = cursor.split(':');
|
|
42
|
-
const from = Y.createAbsolutePositionFromRelativePosition(
|
|
43
|
-
Y.decodeRelativePosition(stringToArray(parts[0])),
|
|
44
|
-
text.doc,
|
|
45
|
-
);
|
|
46
|
-
const to = Y.createAbsolutePositionFromRelativePosition(
|
|
47
|
-
Y.decodeRelativePosition(stringToArray(parts[1])),
|
|
48
|
-
text.doc,
|
|
49
|
-
);
|
|
50
|
-
return from && to ? { from: from.index, to: to.index } : undefined;
|
|
51
|
-
},
|
|
52
31
|
extension: [modelState.init(() => model), yjs(text.content as YText, provider?.awareness)],
|
|
53
32
|
awareness: provider?.awareness,
|
|
54
33
|
peer: identity
|
package/src/index.ts
CHANGED
|
@@ -5,8 +5,11 @@
|
|
|
5
5
|
export { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
|
|
6
6
|
export { Doc, YText, YXmlFragment } from '@dxos/text-model';
|
|
7
7
|
export { type Extension } from '@codemirror/state';
|
|
8
|
+
export { type EditorView } from '@codemirror/view';
|
|
8
9
|
|
|
9
10
|
export * from './components';
|
|
10
11
|
export * from './extensions';
|
|
11
12
|
export * from './hooks';
|
|
13
|
+
export { getToken } from './styles';
|
|
12
14
|
export * from './themes';
|
|
15
|
+
export * from './util';
|
package/src/styles/markdown.ts
CHANGED
|
@@ -39,10 +39,8 @@ export const codeBlock = 'mlb-2 font-mono bg-neutral-500/10 p-3 rounded';
|
|
|
39
39
|
|
|
40
40
|
export const inlineUrl = mx(code, 'px-1');
|
|
41
41
|
|
|
42
|
-
// TODO(burdon): Replace with widget.
|
|
43
42
|
export const blockquote = mx('pl-1 mr-1 border-is-4 border-primary-500/70 dark:border-primary-500/30', light);
|
|
44
43
|
|
|
45
|
-
// TODO(burdon): Replace with widget.
|
|
46
44
|
export const horizontalRule =
|
|
47
45
|
'flex mlb-4 border-b text-neutral-100 dark:text-neutral-900 border-neutral-200 dark:border-neutral-800';
|
|
48
46
|
|
package/src/styles/tokens.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import get from 'lodash.get';
|
|
5
6
|
import type { StyleSpec } from 'style-mod';
|
|
6
7
|
|
|
7
8
|
import { tailwindConfig, type TailwindConfig } from '@dxos/react-ui-theme';
|
|
@@ -12,3 +13,5 @@ export type ThemeStyles = {
|
|
|
12
13
|
|
|
13
14
|
// TODO(thure): Why export the whole theme? Can this be done differently?
|
|
14
15
|
export const tokens: TailwindConfig['theme'] = tailwindConfig({}).theme;
|
|
16
|
+
|
|
17
|
+
export const getToken = (path: string, defaultValue: any = undefined) => get(tokens, path, defaultValue);
|