@dxos/react-ui-editor 0.3.11-main.800163e → 0.3.11-main.80db41b
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 +55 -26
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +6 -5
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +2 -3
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +12 -0
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/package.json +24 -24
- package/src/components/TextEditor/TextEditor.tsx +3 -4
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- package/src/components/Toolbar/Toolbar.tsx +19 -16
- package/src/extensions/awareness.ts +4 -1
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +12 -2
- package/src/extensions/markdown/code.ts +12 -4
- package/src/extensions/markdown/link.ts +8 -2
- package/src/hooks/useTextModel.ts +17 -1
- package/src/index.ts +1 -1
- package/src/themes/default.ts +7 -4
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +0 -26
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
- package/src/components/TextEditor/comments.stories.tsx +0 -357
|
@@ -207,7 +207,10 @@ export class RemoteSelectionsDecorator implements PluginValue {
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
class RemoteCaretWidget extends WidgetType {
|
|
210
|
-
constructor(
|
|
210
|
+
constructor(
|
|
211
|
+
private readonly _name: string,
|
|
212
|
+
private readonly _color: string,
|
|
213
|
+
) {
|
|
211
214
|
super();
|
|
212
215
|
}
|
|
213
216
|
|
package/src/extensions/blast.ts
CHANGED
|
@@ -125,7 +125,10 @@ class Blaster {
|
|
|
125
125
|
|
|
126
126
|
_lastPoint = { x: 0, y: 0 };
|
|
127
127
|
|
|
128
|
-
constructor(
|
|
128
|
+
constructor(
|
|
129
|
+
private readonly _node: HTMLElement,
|
|
130
|
+
private readonly _options: BlastOptions,
|
|
131
|
+
) {
|
|
129
132
|
this._effect = this._options.effect === 1 ? new Effect1(_options) : new Effect2(_options);
|
|
130
133
|
}
|
|
131
134
|
|
|
@@ -18,12 +18,22 @@ import { callbackWrapper } from '../util';
|
|
|
18
18
|
|
|
19
19
|
// TODO(burdon): Reconcile with theme.
|
|
20
20
|
const styles = EditorView.baseTheme({
|
|
21
|
-
'& .cm-comment': {
|
|
21
|
+
'&light .cm-comment, &light .cm-comment-current': { mixBlendMode: 'darken' },
|
|
22
|
+
'&dark .cm-comment, &dark .cm-comment-current': { mixBlendMode: 'plus-lighter' },
|
|
23
|
+
'&light .cm-comment': {
|
|
22
24
|
backgroundColor: getToken('extend.colors.yellow.50'),
|
|
23
25
|
},
|
|
24
|
-
'& .cm-comment-current': {
|
|
26
|
+
'&light .cm-comment-current': {
|
|
25
27
|
backgroundColor: getToken('extend.colors.yellow.100'),
|
|
26
28
|
},
|
|
29
|
+
'&dark .cm-comment': {
|
|
30
|
+
color: getToken('extend.colors.yellow.50'),
|
|
31
|
+
backgroundColor: getToken('extend.colors.yellow.900'),
|
|
32
|
+
},
|
|
33
|
+
'&dark .cm-comment-current': {
|
|
34
|
+
color: getToken('extend.colors.yellow.100'),
|
|
35
|
+
backgroundColor: getToken('extend.colors.yellow.950'),
|
|
36
|
+
},
|
|
27
37
|
});
|
|
28
38
|
|
|
29
39
|
const commentMark = Decoration.mark({ class: 'cm-comment' });
|
|
@@ -21,12 +21,11 @@ import { getToken } from '../../styles';
|
|
|
21
21
|
// TODO(burdon): Reconcile with theme.
|
|
22
22
|
const styles = EditorView.baseTheme({
|
|
23
23
|
'& .cm-code': {
|
|
24
|
-
paddingInline: '1rem !important',
|
|
25
24
|
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
26
25
|
},
|
|
27
26
|
'& .cm-codeblock': {
|
|
28
|
-
display: 'inline-block',
|
|
29
27
|
width: '100%',
|
|
28
|
+
paddingInline: '1rem !important',
|
|
30
29
|
position: 'relative',
|
|
31
30
|
'&::after': {
|
|
32
31
|
content: '""',
|
|
@@ -47,12 +46,14 @@ const styles = EditorView.baseTheme({
|
|
|
47
46
|
},
|
|
48
47
|
},
|
|
49
48
|
'& .cm-codeblock-first': {
|
|
49
|
+
display: 'inline-block',
|
|
50
50
|
'&::after': {
|
|
51
51
|
borderTopLeftRadius: '.5rem',
|
|
52
52
|
borderTopRightRadius: '.5rem',
|
|
53
53
|
},
|
|
54
54
|
},
|
|
55
55
|
'& .cm-codeblock-last': {
|
|
56
|
+
display: 'inline-block',
|
|
56
57
|
'&::after': {
|
|
57
58
|
borderBottomLeftRadius: '.5rem',
|
|
58
59
|
borderBottomRightRadius: '.5rem',
|
|
@@ -67,6 +68,7 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
|
67
68
|
return [start, end];
|
|
68
69
|
};
|
|
69
70
|
|
|
71
|
+
// TODO(burdon): Add copy to clipboard widget.
|
|
70
72
|
class LineWidget extends WidgetType {
|
|
71
73
|
constructor(private readonly _className: string) {
|
|
72
74
|
super();
|
|
@@ -83,17 +85,19 @@ class LineWidget extends WidgetType {
|
|
|
83
85
|
const top = new LineWidget('cm-codeblock-first');
|
|
84
86
|
const bottom = new LineWidget('cm-codeblock-last');
|
|
85
87
|
|
|
88
|
+
// TODO(burdon): Selection isn't visible unless multiline (e.g., can't highlight word).
|
|
89
|
+
|
|
86
90
|
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
87
91
|
const builder = new RangeSetBuilder<Decoration>();
|
|
88
92
|
const { state } = view;
|
|
89
93
|
const blocks = view.viewportLineBlocks;
|
|
90
94
|
const cursor = state.selection.main.head;
|
|
91
95
|
|
|
92
|
-
// TODO(burdon): Add copy to clipboard widget.
|
|
93
96
|
for (const { from, to } of view.visibleRanges) {
|
|
94
97
|
syntaxTree(state).iterate({
|
|
95
98
|
enter: (node) => {
|
|
96
99
|
if (node.name === 'FencedCode') {
|
|
100
|
+
// FencedCode > CodeMark > [CodeInfo] > CodeText > CodeMark
|
|
97
101
|
const edit = !view.state.readOnly && cursor >= node.from && cursor <= node.to;
|
|
98
102
|
const range = getLineRange(blocks, node.from, node.to);
|
|
99
103
|
for (let i = range[0]; i <= range[1]; i++) {
|
|
@@ -105,7 +109,11 @@ const buildDecorations = (view: EditorView): DecorationSet => {
|
|
|
105
109
|
block.from,
|
|
106
110
|
block.from,
|
|
107
111
|
Decoration.line({
|
|
108
|
-
class: mx(
|
|
112
|
+
class: mx(
|
|
113
|
+
'cm-code cm-codeblock',
|
|
114
|
+
i === range[0] && 'cm-codeblock-first',
|
|
115
|
+
i === range[1] && 'cm-codeblock-last',
|
|
116
|
+
),
|
|
109
117
|
}),
|
|
110
118
|
);
|
|
111
119
|
}
|
|
@@ -91,7 +91,10 @@ export const link = (options: LinkOptions = {}): Extension => {
|
|
|
91
91
|
};
|
|
92
92
|
|
|
93
93
|
class LinkButton extends WidgetType {
|
|
94
|
-
constructor(
|
|
94
|
+
constructor(
|
|
95
|
+
private readonly _url: string,
|
|
96
|
+
private readonly _onAttach: LinkOptions['onRender'],
|
|
97
|
+
) {
|
|
95
98
|
super();
|
|
96
99
|
}
|
|
97
100
|
|
|
@@ -107,7 +110,10 @@ class LinkButton extends WidgetType {
|
|
|
107
110
|
}
|
|
108
111
|
|
|
109
112
|
class LinkText extends WidgetType {
|
|
110
|
-
constructor(
|
|
113
|
+
constructor(
|
|
114
|
+
private readonly _text: string,
|
|
115
|
+
private readonly _url?: string,
|
|
116
|
+
) {
|
|
111
117
|
super();
|
|
112
118
|
}
|
|
113
119
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import get from 'lodash.get';
|
|
6
|
-
import { useEffect, useState } from 'react';
|
|
6
|
+
import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';
|
|
7
7
|
|
|
8
8
|
import { generateName } from '@dxos/display-name';
|
|
9
9
|
import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
@@ -30,6 +30,22 @@ export const useTextModel = (props: UseTextModelProps): EditorModel | undefined
|
|
|
30
30
|
return model;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* For use primarily in stories & tests so the dependence on TextObject can be avoided.
|
|
35
|
+
* @param id
|
|
36
|
+
* @param defaultContent
|
|
37
|
+
*/
|
|
38
|
+
export const useInMemoryTextModel = ({
|
|
39
|
+
id,
|
|
40
|
+
defaultContent,
|
|
41
|
+
}: {
|
|
42
|
+
id: string;
|
|
43
|
+
defaultContent?: string;
|
|
44
|
+
}): EditorModel & { setContent: Dispatch<SetStateAction<string>> } => {
|
|
45
|
+
const [content, setContent] = useState(defaultContent ?? '');
|
|
46
|
+
return { id, content, setContent, text: () => content };
|
|
47
|
+
};
|
|
48
|
+
|
|
33
49
|
const createModel = ({ space, identity, text }: UseTextModelProps) => {
|
|
34
50
|
invariant(isAutomergeObject(text));
|
|
35
51
|
const obj = text as any as AutomergeTextCompat;
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
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
|
+
export { type EditorView, keymap } from '@codemirror/view';
|
|
9
9
|
|
|
10
10
|
export * from './components';
|
|
11
11
|
export * from './extensions';
|
package/src/themes/default.ts
CHANGED
|
@@ -62,11 +62,11 @@ export const defaultTheme: ThemeStyles = {
|
|
|
62
62
|
fontSize: '16px',
|
|
63
63
|
},
|
|
64
64
|
'&light .cm-content': {
|
|
65
|
-
color: get(tokens, 'extend.
|
|
65
|
+
color: get(tokens, 'extend.semanticColors.base.fg.light', 'black'),
|
|
66
66
|
caretColor: 'black',
|
|
67
67
|
},
|
|
68
68
|
'&dark .cm-content': {
|
|
69
|
-
color: get(tokens, 'extend.
|
|
69
|
+
color: get(tokens, 'extend.semanticColors.base.fg.dark', 'white'),
|
|
70
70
|
caretColor: 'white',
|
|
71
71
|
},
|
|
72
72
|
|
|
@@ -79,8 +79,11 @@ export const defaultTheme: ThemeStyles = {
|
|
|
79
79
|
'&dark .cm-cursor, &dark .cm-dropCursor': {
|
|
80
80
|
borderLeft: '2px solid white',
|
|
81
81
|
},
|
|
82
|
-
'.cm-placeholder': {
|
|
83
|
-
|
|
82
|
+
'&light .cm-placeholder': {
|
|
83
|
+
color: get(tokens, 'extend.semanticColors.description.light', 'rgba(0,0,0,.2)'),
|
|
84
|
+
},
|
|
85
|
+
'&dark .cm-placeholder': {
|
|
86
|
+
color: get(tokens, 'extend.semanticColors.description.dark', 'rgba(255,255,255,.2)'),
|
|
84
87
|
},
|
|
85
88
|
|
|
86
89
|
//
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import '@dxosTheme';
|
|
2
|
-
import { type EditorView } from '@codemirror/view';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
type StoryProps = {
|
|
5
|
-
text?: string;
|
|
6
|
-
autoCreate?: boolean;
|
|
7
|
-
};
|
|
8
|
-
declare const _default: {
|
|
9
|
-
title: string;
|
|
10
|
-
component: React.ForwardRefExoticComponent<import("./TextEditor").TextEditorProps & React.RefAttributes<EditorView>>;
|
|
11
|
-
decorators: import("@storybook/react").Decorator[];
|
|
12
|
-
render: (args: StoryProps) => JSX.Element;
|
|
13
|
-
};
|
|
14
|
-
export default _default;
|
|
15
|
-
export declare const Default: {
|
|
16
|
-
args: {
|
|
17
|
-
text: string;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
export declare const Testing: {
|
|
21
|
-
args: {
|
|
22
|
-
text: string;
|
|
23
|
-
autoCreate: boolean;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
//# sourceMappingURL=comments.stories.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"comments.stories.d.ts","sourceRoot":"","sources":["../../../../../src/components/TextEditor/comments.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,YAAY,CAAC;AAEpB,OAAO,EAAE,KAAK,UAAU,EAAU,MAAM,kBAAkB,CAAC;AAG3D,OAAO,KAAwD,MAAM,OAAO,CAAC;AA6M7E,KAAK,UAAU,GAAG;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;;;;;mBAoHe,UAAU;;AAJ3B,wBAKE;AAWF,eAAO,MAAM,OAAO;;;;CAInB,CAAC;AAEF,eAAO,MAAM,OAAO;;;;;CAKnB,CAAC"}
|
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxosTheme';
|
|
6
|
-
|
|
7
|
-
import { type EditorView, keymap } from '@codemirror/view';
|
|
8
|
-
import { faker } from '@faker-js/faker';
|
|
9
|
-
import { Check, Trash } from '@phosphor-icons/react';
|
|
10
|
-
import React, { type FC, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
|
-
|
|
12
|
-
import { getTextContent, TextObject } from '@dxos/echo-schema';
|
|
13
|
-
import { PublicKey } from '@dxos/keys';
|
|
14
|
-
import { log } from '@dxos/log';
|
|
15
|
-
import { Button, DensityProvider } from '@dxos/react-ui';
|
|
16
|
-
import { fixedInsetFlexLayout, mx } from '@dxos/react-ui-theme';
|
|
17
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
18
|
-
|
|
19
|
-
import { MarkdownEditor, TextEditor } from './TextEditor';
|
|
20
|
-
import { comments, type CommentsOptions, focusComment, useComments } from '../../extensions';
|
|
21
|
-
import { type Comment, type Range, useTextModel } from '../../hooks';
|
|
22
|
-
|
|
23
|
-
faker.seed(101);
|
|
24
|
-
|
|
25
|
-
//
|
|
26
|
-
// Editor
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
const Editor: FC<{
|
|
30
|
-
item: { text: TextObject };
|
|
31
|
-
comments: Comment[];
|
|
32
|
-
selected?: string;
|
|
33
|
-
onCreateComment: CommentsOptions['onCreate'];
|
|
34
|
-
onDeleteComment: CommentsOptions['onDelete'];
|
|
35
|
-
onUpdateComment: CommentsOptions['onUpdate'];
|
|
36
|
-
onSelectComment: CommentsOptions['onSelect'];
|
|
37
|
-
}> = ({
|
|
38
|
-
item,
|
|
39
|
-
selected: selectedValue,
|
|
40
|
-
comments: commentRanges,
|
|
41
|
-
onCreateComment,
|
|
42
|
-
onDeleteComment,
|
|
43
|
-
onUpdateComment,
|
|
44
|
-
onSelectComment,
|
|
45
|
-
}) => {
|
|
46
|
-
const model = useTextModel({ text: item.text });
|
|
47
|
-
const view = useRef<EditorView>(null);
|
|
48
|
-
const [selected, setSelected] = useState<string>();
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
if (!view.current?.hasFocus && selectedValue !== selected) {
|
|
51
|
-
setSelected(selectedValue);
|
|
52
|
-
if (selectedValue) {
|
|
53
|
-
focusComment(view.current!, selectedValue);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}, [selected, commentRanges, selectedValue]);
|
|
57
|
-
|
|
58
|
-
useComments(view.current, commentRanges);
|
|
59
|
-
|
|
60
|
-
const extensions = useMemo(() => {
|
|
61
|
-
return [
|
|
62
|
-
comments({
|
|
63
|
-
onCreate: onCreateComment,
|
|
64
|
-
onDelete: onDeleteComment,
|
|
65
|
-
onUpdate: onUpdateComment,
|
|
66
|
-
onSelect: onSelectComment,
|
|
67
|
-
}),
|
|
68
|
-
];
|
|
69
|
-
}, []);
|
|
70
|
-
|
|
71
|
-
if (!model) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// TODO(burdon): Highlight currently selected comment.
|
|
76
|
-
return (
|
|
77
|
-
<div className='flex grow overflow-y-scroll'>
|
|
78
|
-
<MarkdownEditor ref={view} model={model} extensions={extensions} />
|
|
79
|
-
</div>
|
|
80
|
-
);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
//
|
|
84
|
-
// Comments thread
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
type CommentThread = {
|
|
88
|
-
id: string;
|
|
89
|
-
cursor?: string;
|
|
90
|
-
range?: Range;
|
|
91
|
-
yPos?: number;
|
|
92
|
-
messages: TextObject[];
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const Thread: FC<{
|
|
96
|
-
thread: CommentThread;
|
|
97
|
-
selected: boolean;
|
|
98
|
-
onSelect: () => void;
|
|
99
|
-
onResolve: () => void;
|
|
100
|
-
}> = ({ thread, selected, onSelect, onResolve }) => {
|
|
101
|
-
const [focus, setFocus] = useState(false);
|
|
102
|
-
const [item, setItem] = useState({ text: new TextObject() });
|
|
103
|
-
const model = useTextModel({ text: item.text });
|
|
104
|
-
const editorRef = useRef<EditorView>(null);
|
|
105
|
-
|
|
106
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
107
|
-
useEffect(() => {
|
|
108
|
-
if (selected) {
|
|
109
|
-
containerRef.current?.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
|
110
|
-
if (thread.messages.length === 0) {
|
|
111
|
-
setFocus(true);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}, [selected]);
|
|
115
|
-
|
|
116
|
-
const handleCreateMessage = () => {
|
|
117
|
-
const text = model?.text().trim();
|
|
118
|
-
if (text?.length) {
|
|
119
|
-
thread.messages.push(item.text);
|
|
120
|
-
setItem({ text: new TextObject() });
|
|
121
|
-
setFocus(true);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
if (!model) {
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return (
|
|
130
|
-
<div
|
|
131
|
-
className={mx(
|
|
132
|
-
'flex flex-col m-1 rounded shadow divide-y bg-white',
|
|
133
|
-
selected && 'ring',
|
|
134
|
-
!thread.cursor && 'opacity-50',
|
|
135
|
-
)}
|
|
136
|
-
>
|
|
137
|
-
<div className='flex p-2 gap-2 items-center text-xs font-mono text-neutral-500 font-thin'>
|
|
138
|
-
<span>id:{thread.id.slice(0, 4)}</span>
|
|
139
|
-
<span>from:{thread.range?.from}</span>
|
|
140
|
-
<span>to:{thread.range?.to}</span>
|
|
141
|
-
<span>y:{thread.yPos}</span>
|
|
142
|
-
<span className='grow' />
|
|
143
|
-
{!thread.cursor && <Trash />}
|
|
144
|
-
</div>
|
|
145
|
-
|
|
146
|
-
{thread.messages.map((message, i) => (
|
|
147
|
-
// TODO(burdon): Fix default editor padding so content doesn't jump on creating message.
|
|
148
|
-
<div key={i} className='p-2' onClick={() => onSelect()}>
|
|
149
|
-
{getTextContent(message)}
|
|
150
|
-
</div>
|
|
151
|
-
))}
|
|
152
|
-
|
|
153
|
-
<div ref={containerRef} className='flex' onClick={() => onSelect()}>
|
|
154
|
-
<TextEditor
|
|
155
|
-
ref={editorRef}
|
|
156
|
-
autoFocus={focus}
|
|
157
|
-
model={model}
|
|
158
|
-
placeholder={'Enter comment...'}
|
|
159
|
-
slots={{ root: { className: 'grow rounded-b' } }}
|
|
160
|
-
extensions={[
|
|
161
|
-
keymap.of([
|
|
162
|
-
{
|
|
163
|
-
key: 'Enter',
|
|
164
|
-
run: () => {
|
|
165
|
-
handleCreateMessage();
|
|
166
|
-
return true;
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
]),
|
|
170
|
-
]}
|
|
171
|
-
/>
|
|
172
|
-
<Button variant='ghost' classNames='px-1' title='Resolve' onClick={onResolve}>
|
|
173
|
-
<Check />
|
|
174
|
-
</Button>
|
|
175
|
-
</div>
|
|
176
|
-
</div>
|
|
177
|
-
);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const Sidebar: FC<{
|
|
181
|
-
threads: CommentThread[];
|
|
182
|
-
selected?: string;
|
|
183
|
-
onSelect: (thread: string) => void;
|
|
184
|
-
onResolve: (thread: string) => void;
|
|
185
|
-
}> = ({ threads, selected, onSelect, onResolve }) => {
|
|
186
|
-
// Sort by y-position.
|
|
187
|
-
const sortedThreads = useMemo(() => {
|
|
188
|
-
const sorted = [...threads];
|
|
189
|
-
return sorted.sort(({ yPos: a = Infinity }, { yPos: b = Infinity }) => {
|
|
190
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
191
|
-
});
|
|
192
|
-
}, [threads]);
|
|
193
|
-
|
|
194
|
-
return (
|
|
195
|
-
<DensityProvider density='fine'>
|
|
196
|
-
<div className='flex flex-col grow overflow-y-scroll py-4 gap-4'>
|
|
197
|
-
{sortedThreads.map((thread) => (
|
|
198
|
-
<Thread
|
|
199
|
-
key={thread.id}
|
|
200
|
-
thread={thread}
|
|
201
|
-
selected={thread.id === selected}
|
|
202
|
-
onSelect={() => onSelect(thread.id)}
|
|
203
|
-
onResolve={() => onResolve(thread.id)}
|
|
204
|
-
/>
|
|
205
|
-
))}
|
|
206
|
-
</div>
|
|
207
|
-
</DensityProvider>
|
|
208
|
-
);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
//
|
|
212
|
-
// Story container.
|
|
213
|
-
//
|
|
214
|
-
|
|
215
|
-
type StoryProps = {
|
|
216
|
-
text?: string;
|
|
217
|
-
autoCreate?: boolean;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const Story = ({ text, autoCreate }: StoryProps) => {
|
|
221
|
-
const [item] = useState({ text: new TextObject(text) });
|
|
222
|
-
const [threads, setThreads] = useState<CommentThread[]>([]);
|
|
223
|
-
const [selected, setSelected] = useState<string>();
|
|
224
|
-
|
|
225
|
-
const comments = useMemo<Comment[]>(() => threads.map(({ id, cursor }) => ({ id, cursor })), [threads]);
|
|
226
|
-
|
|
227
|
-
// Filter by visibility.
|
|
228
|
-
const visibleThreads = useMemo(() => threads.filter((thread) => thread.yPos !== undefined), [threads]);
|
|
229
|
-
|
|
230
|
-
const handleCreateComment: CommentsOptions['onCreate'] = (cursor, location) => {
|
|
231
|
-
const id = PublicKey.random().toHex();
|
|
232
|
-
log.info('create', { id: id.slice(0, 4), cursor });
|
|
233
|
-
setThreads((threads) => [
|
|
234
|
-
...threads,
|
|
235
|
-
{
|
|
236
|
-
id,
|
|
237
|
-
cursor,
|
|
238
|
-
yPos: location ? Math.floor(location.top) : undefined,
|
|
239
|
-
messages: autoCreate
|
|
240
|
-
? faker.helpers.multiple(() => new TextObject(faker.lorem.sentence()), { count: { min: 1, max: 3 } })
|
|
241
|
-
: [],
|
|
242
|
-
},
|
|
243
|
-
]);
|
|
244
|
-
setSelected(id); // TODO(burdon): Not required.
|
|
245
|
-
return id;
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const handleDeleteComment: CommentsOptions['onDelete'] = (id) => {
|
|
249
|
-
log.info('delete', { id: id.slice(0, 4) });
|
|
250
|
-
setThreads((threads) =>
|
|
251
|
-
threads.map((thread) => {
|
|
252
|
-
if (thread.id === id) {
|
|
253
|
-
thread.cursor = undefined;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return thread;
|
|
257
|
-
}),
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
setSelected((selected) => (selected === id ? undefined : selected));
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const handleUpdateComment: CommentsOptions['onUpdate'] = (id, cursor) => {
|
|
264
|
-
log.info('update', { id: id.slice(0, 4), cursor });
|
|
265
|
-
setThreads((threads) =>
|
|
266
|
-
threads.map((thread) => {
|
|
267
|
-
if (thread.id === id) {
|
|
268
|
-
thread.cursor = cursor;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return thread;
|
|
272
|
-
}),
|
|
273
|
-
);
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const handleSelectComment: CommentsOptions['onSelect'] = ({ comments, selection: { current, closest } }) => {
|
|
277
|
-
log.info('select', { active: current?.slice(0, 4), closest: closest?.slice(0, 4) });
|
|
278
|
-
setThreads((threads) =>
|
|
279
|
-
threads.map((thread) => {
|
|
280
|
-
const comment = comments.find(({ comment }) => comment.id === thread.id);
|
|
281
|
-
if (comment) {
|
|
282
|
-
thread.yPos = comment.location ? Math.floor(comment.location.top) : undefined;
|
|
283
|
-
thread.range = { from: comment.range.from, to: comment.range.to };
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return thread;
|
|
287
|
-
}),
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
setSelected(current ?? closest);
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
const handleSelectThread = (id: string) => {
|
|
294
|
-
setSelected(id);
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
const handleResolveThread = (id: string) => {
|
|
298
|
-
setThreads((threads) => [...threads.filter((thread) => thread.id !== id)]);
|
|
299
|
-
setSelected(undefined);
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
return (
|
|
303
|
-
<div className={mx(fixedInsetFlexLayout, 'bg-neutral-100')}>
|
|
304
|
-
<div className='flex justify-center h-full gap-8'>
|
|
305
|
-
<div className='flex flex-col h-full w-[600px]'>
|
|
306
|
-
<Editor
|
|
307
|
-
item={item}
|
|
308
|
-
selected={selected}
|
|
309
|
-
comments={comments}
|
|
310
|
-
onCreateComment={handleCreateComment}
|
|
311
|
-
onDeleteComment={handleDeleteComment}
|
|
312
|
-
onUpdateComment={handleUpdateComment}
|
|
313
|
-
onSelectComment={handleSelectComment}
|
|
314
|
-
/>
|
|
315
|
-
</div>
|
|
316
|
-
|
|
317
|
-
<div className='flex flex-col h-full w-[300px]'>
|
|
318
|
-
<Sidebar
|
|
319
|
-
threads={visibleThreads}
|
|
320
|
-
selected={selected}
|
|
321
|
-
onSelect={handleSelectThread}
|
|
322
|
-
onResolve={handleResolveThread}
|
|
323
|
-
/>
|
|
324
|
-
</div>
|
|
325
|
-
</div>
|
|
326
|
-
</div>
|
|
327
|
-
);
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
export default {
|
|
331
|
-
title: 'react-ui-editor/comments',
|
|
332
|
-
component: MarkdownEditor,
|
|
333
|
-
decorators: [withTheme],
|
|
334
|
-
render: (args: StoryProps) => <Story {...args} />,
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const str = (...lines: string[]) => lines.join('\n');
|
|
338
|
-
|
|
339
|
-
const document = str(
|
|
340
|
-
'# Comments',
|
|
341
|
-
'',
|
|
342
|
-
str(...faker.helpers.multiple(() => [faker.lorem.paragraph({ min: 5, max: 10 }), ''], { count: 20 }).flat()),
|
|
343
|
-
'',
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
export const Default = {
|
|
347
|
-
args: {
|
|
348
|
-
text: document,
|
|
349
|
-
},
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
export const Testing = {
|
|
353
|
-
args: {
|
|
354
|
-
text: document,
|
|
355
|
-
autoCreate: true,
|
|
356
|
-
},
|
|
357
|
-
};
|