@dxos/react-ui-editor 0.3.11-main.fbbdc2a → 0.3.11-main.fc97a54
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 +2904 -1394
- 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 +16 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
- 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/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts +23 -0
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +29 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +25 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +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/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
- 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 +9 -11
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +41 -20
- 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/index.d.ts +2 -8
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts +58 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.test.d.ts +3 -0
- package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
- package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +8 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.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/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +4 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -32
- package/dist/types/src/hooks/useTextModel.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 +2 -2
- 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 +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +48 -37
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +85 -92
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +135 -95
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- package/src/components/TextEditor/hooks.stories.tsx +111 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +102 -0
- package/src/components/Toolbar/Toolbar.tsx +264 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/autocomplete.ts +23 -19
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +55 -46
- package/src/extensions/automerge/cursor.ts +5 -4
- package/src/extensions/automerge/defs.ts +13 -34
- package/src/extensions/automerge/semaphore.ts +33 -32
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +46 -49
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +332 -267
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/index.ts +2 -8
- package/src/extensions/markdown/bundle.ts +23 -28
- package/src/extensions/markdown/code.ts +150 -0
- package/src/extensions/markdown/formatting.test.ts +482 -0
- package/src/extensions/markdown/formatting.ts +1237 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +11 -26
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/image.ts +87 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +90 -83
- package/src/extensions/{table.ts → markdown/table.ts} +10 -4
- package/src/extensions/markdown/tasklist.ts +110 -0
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +5 -2
- package/src/hooks/useActionHandler.ts +93 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +153 -0
- package/src/hooks/useTextModel.ts +67 -60
- package/src/index.ts +3 -0
- package/src/styles/markdown.ts +9 -12
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +59 -83
- package/src/util.ts +41 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- 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/extensions/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.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 +0 -5
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -99
- package/src/extensions/hr.ts +0 -74
- package/src/extensions/image.ts +0 -69
- package/src/extensions/mermaid.ts +0 -11
- package/src/extensions/tasklist.ts +0 -121
- package/src/extensions/yjs/cursor.ts +0 -21
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -178
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- package/src/util/util.ts +0 -18
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type Icon,
|
|
7
|
+
CaretRight,
|
|
8
|
+
ChatText,
|
|
9
|
+
Code,
|
|
10
|
+
CodeBlock,
|
|
11
|
+
Link,
|
|
12
|
+
ListBullets,
|
|
13
|
+
ListChecks,
|
|
14
|
+
ListNumbers,
|
|
15
|
+
Paragraph,
|
|
16
|
+
TextStrikethrough,
|
|
17
|
+
Table,
|
|
18
|
+
TextB,
|
|
19
|
+
TextHOne,
|
|
20
|
+
TextHTwo,
|
|
21
|
+
TextHThree,
|
|
22
|
+
TextHFour,
|
|
23
|
+
TextHFive,
|
|
24
|
+
TextHSix,
|
|
25
|
+
TextItalic,
|
|
26
|
+
} from '@phosphor-icons/react';
|
|
27
|
+
import { createContext } from '@radix-ui/react-context';
|
|
28
|
+
import React, { type PropsWithChildren } from 'react';
|
|
29
|
+
|
|
30
|
+
import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
|
|
31
|
+
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
32
|
+
|
|
33
|
+
import { type Formatting } from '../../extensions';
|
|
34
|
+
|
|
35
|
+
const HeadingIcons: { [key: string]: Icon } = {
|
|
36
|
+
'0': Paragraph,
|
|
37
|
+
'1': TextHOne,
|
|
38
|
+
'2': TextHTwo,
|
|
39
|
+
'3': TextHThree,
|
|
40
|
+
'4': TextHFour,
|
|
41
|
+
'5': TextHFive,
|
|
42
|
+
'6': TextHSix,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// TODO(burdon): Revert to string to make extensible?
|
|
46
|
+
export type ActionType =
|
|
47
|
+
| 'blockquote'
|
|
48
|
+
| 'strong'
|
|
49
|
+
| 'codeblock'
|
|
50
|
+
| 'comment'
|
|
51
|
+
| 'heading'
|
|
52
|
+
| 'image'
|
|
53
|
+
| 'emphasis'
|
|
54
|
+
| 'code'
|
|
55
|
+
| 'link'
|
|
56
|
+
| 'list-bullet'
|
|
57
|
+
| 'list-ordered'
|
|
58
|
+
| 'list-tasks'
|
|
59
|
+
| 'mention'
|
|
60
|
+
| 'prompt'
|
|
61
|
+
| 'strikethrough'
|
|
62
|
+
| 'table';
|
|
63
|
+
|
|
64
|
+
export type Action = {
|
|
65
|
+
type: ActionType;
|
|
66
|
+
data?: any;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export type ToolbarProps = PropsWithChildren<{
|
|
70
|
+
state: Formatting | null;
|
|
71
|
+
onAction?: (action: Action) => void;
|
|
72
|
+
}>;
|
|
73
|
+
|
|
74
|
+
const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
|
|
75
|
+
|
|
76
|
+
const ToolbarRoot = ({ children, onAction, state }: ToolbarProps) => {
|
|
77
|
+
return (
|
|
78
|
+
<ToolbarContextProvider onAction={onAction} state={state}>
|
|
79
|
+
<DensityProvider density='fine'>
|
|
80
|
+
<div role='toolbar' className='flex w-full shrink-0 p-1 gap-4 items-center whitespace-nowrap overflow-hidden'>
|
|
81
|
+
{children}
|
|
82
|
+
</div>
|
|
83
|
+
</DensityProvider>
|
|
84
|
+
</ToolbarContextProvider>
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
type ToolbarButtonProps = {
|
|
89
|
+
Icon: Icon;
|
|
90
|
+
disable?: (state: Formatting) => boolean;
|
|
91
|
+
getState?: (state: Formatting) => boolean;
|
|
92
|
+
onClick: (state: Formatting | null) => Action | undefined;
|
|
93
|
+
} & NonNullable<Pick<ButtonProps, 'title'>>;
|
|
94
|
+
|
|
95
|
+
const ToolbarButton = ({ Icon, onClick, title, getState, disable }: ToolbarButtonProps) => {
|
|
96
|
+
const { onAction, state } = useToolbarContext('ToolbarButton');
|
|
97
|
+
const active = getState && state ? getState(state) : false;
|
|
98
|
+
const disabled = disable && state ? disable(state) : false;
|
|
99
|
+
|
|
100
|
+
const handleClick = (event: React.MouseEvent) => {
|
|
101
|
+
const action = onClick(state);
|
|
102
|
+
if (action) {
|
|
103
|
+
onAction?.(action);
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<Button
|
|
110
|
+
variant='ghost'
|
|
111
|
+
classNames={mx('p-2', active && 'ring-[1px]')}
|
|
112
|
+
onMouseDown={handleClick}
|
|
113
|
+
title={title}
|
|
114
|
+
disabled={disabled}
|
|
115
|
+
>
|
|
116
|
+
<Icon className={getSize(5)} />
|
|
117
|
+
</Button>
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const ToolbarSeparator = () => <div className='grow' />;
|
|
122
|
+
|
|
123
|
+
const MarkdownHeading = () => {
|
|
124
|
+
const { onAction, state } = useToolbarContext('MarkdownFormatting');
|
|
125
|
+
const blockType = state ? state.blockType : 'paragraph';
|
|
126
|
+
const header = blockType && /heading(\d)/.exec(blockType);
|
|
127
|
+
const value = header ? header[1] : blockType === 'paragraph' || !blockType ? '0' : undefined;
|
|
128
|
+
const HeadingIcon = HeadingIcons[value ?? '0'];
|
|
129
|
+
return (
|
|
130
|
+
<Select.Root
|
|
131
|
+
disabled={value === null}
|
|
132
|
+
value={value ?? '0'}
|
|
133
|
+
onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
|
|
134
|
+
>
|
|
135
|
+
<Select.TriggerButton>
|
|
136
|
+
<HeadingIcon className={getSize(5)} />
|
|
137
|
+
</Select.TriggerButton>
|
|
138
|
+
<Select.Portal>
|
|
139
|
+
<Select.Content>
|
|
140
|
+
<Select.Viewport>
|
|
141
|
+
<Select.Option value='0'>Paragraph</Select.Option>
|
|
142
|
+
{[1, 2, 3, 4, 5, 6].map((level) => (
|
|
143
|
+
<Select.Option key={level} value={String(level)}>
|
|
144
|
+
Heading {level}
|
|
145
|
+
</Select.Option>
|
|
146
|
+
))}
|
|
147
|
+
</Select.Viewport>
|
|
148
|
+
</Select.Content>
|
|
149
|
+
</Select.Portal>
|
|
150
|
+
</Select.Root>
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const MarkdownStyles = () => (
|
|
155
|
+
<div role='none'>
|
|
156
|
+
<ToolbarButton
|
|
157
|
+
Icon={TextB}
|
|
158
|
+
title='String'
|
|
159
|
+
disable={(s) => s.blockType === 'codeblock'}
|
|
160
|
+
getState={(s) => s.strong}
|
|
161
|
+
onClick={(s) => ({ type: 'strong', data: s ? !s.strong : null })}
|
|
162
|
+
/>
|
|
163
|
+
<ToolbarButton
|
|
164
|
+
Icon={TextItalic}
|
|
165
|
+
title='Emphasis'
|
|
166
|
+
disable={(s) => s.blockType === 'codeblock'}
|
|
167
|
+
getState={(s) => s.emphasis}
|
|
168
|
+
onClick={(s) => ({ type: 'emphasis', data: s ? !s.emphasis : null })}
|
|
169
|
+
/>
|
|
170
|
+
<ToolbarButton
|
|
171
|
+
Icon={TextStrikethrough}
|
|
172
|
+
title='Strike-through'
|
|
173
|
+
disable={(s) => s.blockType === 'codeblock'}
|
|
174
|
+
getState={(s) => s.strikethrough}
|
|
175
|
+
onClick={(s) => ({ type: 'strikethrough', data: s ? !s.strikethrough : null })}
|
|
176
|
+
/>
|
|
177
|
+
<ToolbarButton
|
|
178
|
+
Icon={Code}
|
|
179
|
+
title='Inline code'
|
|
180
|
+
disable={(s) => s.blockType === 'codeblock'}
|
|
181
|
+
getState={(s) => s.code}
|
|
182
|
+
onClick={(s) => ({ type: 'code', data: s ? !s.code : null })}
|
|
183
|
+
/>
|
|
184
|
+
</div>
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const MarkdownLists = () => (
|
|
188
|
+
<div role='none'>
|
|
189
|
+
<ToolbarButton
|
|
190
|
+
Icon={ListBullets}
|
|
191
|
+
title='Bullet list'
|
|
192
|
+
getState={(s) => s.listStyle === 'bullet'}
|
|
193
|
+
onClick={(s) => ({ type: 'list-bullet', data: s ? s.listStyle !== 'bullet' : null })}
|
|
194
|
+
/>
|
|
195
|
+
<ToolbarButton
|
|
196
|
+
Icon={ListNumbers}
|
|
197
|
+
title='Numbered list'
|
|
198
|
+
getState={(s) => s.listStyle === 'ordered'}
|
|
199
|
+
onClick={(s) => ({ type: 'list-ordered', data: s ? s.listStyle !== 'ordered' : null })}
|
|
200
|
+
/>
|
|
201
|
+
<ToolbarButton
|
|
202
|
+
Icon={ListChecks}
|
|
203
|
+
title='Task list'
|
|
204
|
+
getState={(s) => s.listStyle === 'task'}
|
|
205
|
+
onClick={(s) => ({ type: 'list-tasks', data: s ? s.listStyle !== 'task' : null })}
|
|
206
|
+
/>
|
|
207
|
+
</div>
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
const MarkdownBlocks = () => (
|
|
211
|
+
<div role='none'>
|
|
212
|
+
<ToolbarButton
|
|
213
|
+
Icon={CaretRight}
|
|
214
|
+
title='Block quote'
|
|
215
|
+
getState={(s) => s.blockQuote}
|
|
216
|
+
onClick={(s) => ({ type: 'blockquote', data: s ? !s.blockQuote : null })}
|
|
217
|
+
/>
|
|
218
|
+
<ToolbarButton
|
|
219
|
+
Icon={CodeBlock}
|
|
220
|
+
title='Code block'
|
|
221
|
+
disable={(s) => !s.blankLine}
|
|
222
|
+
getState={(s) => s.blockType === 'codeblock'}
|
|
223
|
+
onClick={(s) => ({ type: 'codeblock', data: s ? s.blockType !== 'codeblock' : null })}
|
|
224
|
+
/>
|
|
225
|
+
<ToolbarButton Icon={Table} title='Table' disable={(s) => !s.blankLine} onClick={() => ({ type: 'table' })} />
|
|
226
|
+
</div>
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const MarkdownLinks = () => (
|
|
230
|
+
<div role='none'>
|
|
231
|
+
<ToolbarButton
|
|
232
|
+
Icon={Link}
|
|
233
|
+
title='Link'
|
|
234
|
+
getState={(s) => s.link}
|
|
235
|
+
onClick={(s) => ({ type: 'link', data: s ? !s.link : null })}
|
|
236
|
+
/>
|
|
237
|
+
{/* <ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} /> */}
|
|
238
|
+
{/* <ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} /> */}
|
|
239
|
+
</div>
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const MarkdownStandard = () => (
|
|
243
|
+
<>
|
|
244
|
+
<MarkdownHeading />
|
|
245
|
+
<MarkdownStyles />
|
|
246
|
+
<MarkdownLists />
|
|
247
|
+
<MarkdownBlocks />
|
|
248
|
+
<MarkdownLinks />
|
|
249
|
+
</>
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const MarkdownExtended = () => (
|
|
253
|
+
<Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
export const Toolbar = {
|
|
257
|
+
Root: ToolbarRoot,
|
|
258
|
+
Button: ToolbarButton,
|
|
259
|
+
Separator: ToolbarSeparator,
|
|
260
|
+
Markdown: MarkdownStandard,
|
|
261
|
+
Extended: MarkdownExtended,
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
export { useToolbarContext };
|
package/src/components/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type CompletionContext,
|
|
14
14
|
type CompletionResult,
|
|
15
15
|
} from '@codemirror/autocomplete';
|
|
16
|
+
import { markdownLanguage } from '@codemirror/lang-markdown';
|
|
16
17
|
import { keymap } from '@codemirror/view';
|
|
17
18
|
|
|
18
19
|
export type AutocompleteResult = Completion;
|
|
@@ -28,32 +29,35 @@ export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
|
|
|
28
29
|
return [
|
|
29
30
|
// https://codemirror.net/docs/ref/#view.keymap
|
|
30
31
|
// https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
|
|
32
|
+
// TODO(burdon): Set custom keymap.
|
|
31
33
|
keymap.of(completionKeymap),
|
|
32
34
|
|
|
33
35
|
// https://codemirror.net/examples/autocompletion
|
|
34
36
|
// https://codemirror.net/docs/ref/#autocomplete.autocompletion
|
|
35
37
|
autocompletion({
|
|
36
|
-
|
|
38
|
+
activateOnTyping: true,
|
|
39
|
+
|
|
40
|
+
// closeOnBlur: false,
|
|
37
41
|
// defaultKeymap: false,
|
|
38
42
|
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
43
|
+
// TODO(burdon): Styles/fragments.
|
|
44
|
+
tooltipClass: () => 'shadow rounded',
|
|
45
|
+
}),
|
|
46
|
+
|
|
47
|
+
// TODO(burdon): Option to create new page?
|
|
48
|
+
// TODO(burdon): Optional decoration via addToOptions
|
|
49
|
+
markdownLanguage.data.of({
|
|
50
|
+
autocomplete: (context: CompletionContext): CompletionResult | null => {
|
|
51
|
+
const match = context.matchBefore(/\w*/);
|
|
52
|
+
if (!match || (match.from === match.to && !context.explicit)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
from: match.from,
|
|
58
|
+
options: onSearch(match.text.toLowerCase()),
|
|
59
|
+
};
|
|
60
|
+
},
|
|
57
61
|
}),
|
|
58
62
|
];
|
|
59
63
|
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { EditorState } from '@codemirror/state';
|
|
6
|
+
import { EditorView } from '@codemirror/view';
|
|
7
|
+
import { render, screen } from '@testing-library/react';
|
|
8
|
+
import chai, { expect } from 'chai';
|
|
9
|
+
import chaiDom from 'chai-dom';
|
|
10
|
+
import get from 'lodash.get';
|
|
11
|
+
import React, { type FC, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { describe, test } from 'vitest';
|
|
13
|
+
|
|
14
|
+
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
15
|
+
|
|
16
|
+
import { automerge } from './automerge';
|
|
17
|
+
|
|
18
|
+
type TestObject = {
|
|
19
|
+
text: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const path = ['text'];
|
|
23
|
+
|
|
24
|
+
class Generator {
|
|
25
|
+
constructor(private readonly _handle: DocHandle<TestObject>) {}
|
|
26
|
+
update(text: string) {
|
|
27
|
+
this._handle.change((doc: TestObject) => {
|
|
28
|
+
doc.text = text;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Test: FC<{ handle: DocHandle<TestObject>; generator: Generator }> = ({ handle, generator }) => {
|
|
34
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
35
|
+
const [view, setView] = useState<EditorView>();
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const extensions = [
|
|
38
|
+
automerge({ handle, path }),
|
|
39
|
+
EditorView.updateListener.of((update) => {
|
|
40
|
+
if (view.state.doc.toString() === 'hello!') {
|
|
41
|
+
// Update editor.
|
|
42
|
+
update.view.dispatch({ changes: { from: view.state.doc.length - 1, insert: ' world' } });
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const view = new EditorView({
|
|
48
|
+
state: EditorState.create({ doc: get(handle.docSync()!, path), extensions }),
|
|
49
|
+
parent: ref.current!,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
setView(view);
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {}, [view]);
|
|
56
|
+
|
|
57
|
+
return <div ref={ref} />;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
chai.use(chaiDom);
|
|
61
|
+
|
|
62
|
+
describe('Automerge', () => {
|
|
63
|
+
test('basic sync', () => {
|
|
64
|
+
const repo = new Repo({ network: [] });
|
|
65
|
+
const handle = repo.create<TestObject>();
|
|
66
|
+
const generator = new Generator(handle);
|
|
67
|
+
render(<Test handle={handle} generator={generator} />);
|
|
68
|
+
expect(screen.getByRole('textbox')).to.have.text('');
|
|
69
|
+
|
|
70
|
+
generator.update('hello!');
|
|
71
|
+
expect(screen.getByRole('textbox')).to.have.text('hello world!');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// TODO(burdon): Test history/undo.
|
|
75
|
+
// TODO(burdon): https://testing-library.com/docs/react-testing-library/example-intro/
|
|
76
|
+
});
|
|
@@ -4,20 +4,16 @@
|
|
|
4
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
5
|
//
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { invertedEffects } from '@codemirror/commands';
|
|
8
|
+
import { StateField, type Extension, type StateEffect } from '@codemirror/state';
|
|
9
|
+
import { EditorView, ViewPlugin } from '@codemirror/view';
|
|
9
10
|
|
|
10
|
-
import { next as A } from '@dxos/automerge/automerge';
|
|
11
|
-
import { type Prop } from '@dxos/automerge/automerge';
|
|
11
|
+
import { next as A, type Prop } from '@dxos/automerge/automerge';
|
|
12
12
|
|
|
13
13
|
import { cursorConverter } from './cursor';
|
|
14
|
-
import {
|
|
14
|
+
import { updateHeadsEffect, type IDocHandle, isReconcile, type State } from './defs';
|
|
15
15
|
import { PatchSemaphore } from './semaphore';
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
19
|
-
combine: (values) => values.at(-1)!, // Take last.
|
|
20
|
-
});
|
|
16
|
+
import { Cursor } from '../cursor';
|
|
21
17
|
|
|
22
18
|
export type AutomergeOptions = {
|
|
23
19
|
handle: IDocHandle;
|
|
@@ -25,70 +21,83 @@ export type AutomergeOptions = {
|
|
|
25
21
|
};
|
|
26
22
|
|
|
27
23
|
export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
28
|
-
|
|
29
|
-
const stateField: StateField<Value> = StateField.define({
|
|
24
|
+
const syncState = StateField.define<State>({
|
|
30
25
|
create: () => ({
|
|
26
|
+
path: path.slice(),
|
|
31
27
|
lastHeads: A.getHeads(handle.docSync()!),
|
|
32
28
|
unreconciledTransactions: [],
|
|
33
|
-
path: path.slice(),
|
|
34
29
|
}),
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
|
|
31
|
+
update: (value, tr) => {
|
|
32
|
+
const result: State = {
|
|
33
|
+
path: path.slice(),
|
|
37
34
|
lastHeads: value.lastHeads,
|
|
38
35
|
unreconciledTransactions: value.unreconciledTransactions.slice(),
|
|
39
|
-
path: path.slice(),
|
|
40
36
|
};
|
|
37
|
+
|
|
41
38
|
let clearUnreconciled = false;
|
|
42
39
|
for (const effect of tr.effects) {
|
|
43
|
-
if (effect.is(
|
|
40
|
+
if (effect.is(updateHeadsEffect)) {
|
|
44
41
|
result.lastHeads = effect.value.newHeads;
|
|
45
42
|
clearUnreconciled = true;
|
|
46
43
|
}
|
|
47
44
|
}
|
|
45
|
+
|
|
48
46
|
if (clearUnreconciled) {
|
|
49
47
|
result.unreconciledTransactions = [];
|
|
50
48
|
} else {
|
|
51
|
-
if (!
|
|
49
|
+
if (!isReconcile(tr)) {
|
|
52
50
|
result.unreconciledTransactions.push(tr);
|
|
53
51
|
}
|
|
54
52
|
}
|
|
53
|
+
|
|
55
54
|
return result;
|
|
56
55
|
},
|
|
57
56
|
});
|
|
58
57
|
|
|
59
|
-
const semaphore = new PatchSemaphore(
|
|
58
|
+
const semaphore = new PatchSemaphore(handle, syncState);
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
handle.addListener('change', this._handleChange);
|
|
65
|
-
}
|
|
60
|
+
return [
|
|
61
|
+
Cursor.converter.of(cursorConverter(handle, path)),
|
|
62
|
+
syncState,
|
|
66
63
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
64
|
+
// Reconcile external updates.
|
|
65
|
+
ViewPlugin.fromClass(
|
|
66
|
+
class {
|
|
67
|
+
constructor(private readonly _view: EditorView) {
|
|
68
|
+
handle.addListener('change', this._handleChange.bind(this));
|
|
73
69
|
}
|
|
70
|
+
|
|
71
|
+
destroy() {
|
|
72
|
+
handle.removeListener('change', this._handleChange.bind(this));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_handleChange() {
|
|
76
|
+
semaphore.reconcile(this._view);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
),
|
|
80
|
+
|
|
81
|
+
// Reconcile local updates.
|
|
82
|
+
EditorView.updateListener.of(({ view, changes }) => {
|
|
83
|
+
if (!changes.empty) {
|
|
84
|
+
semaphore.reconcile(view);
|
|
74
85
|
}
|
|
86
|
+
}),
|
|
75
87
|
|
|
76
|
-
|
|
77
|
-
|
|
88
|
+
// TODO(burdon): Record undo transactions.
|
|
89
|
+
// See https://github.com/yjs/y-codemirror.next/tree/main
|
|
90
|
+
// https://codemirror.net/examples/inverted-effect
|
|
91
|
+
invertedEffects.of((tr) => {
|
|
92
|
+
// Each change results it three transactions: insert, delete, insert.
|
|
93
|
+
const effects: StateEffect<any>[] = [];
|
|
94
|
+
if (!tr.changes.empty) {
|
|
95
|
+
tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
|
96
|
+
// effects.push(effectType.of({});
|
|
97
|
+
});
|
|
78
98
|
}
|
|
79
99
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
},
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
extension: [
|
|
88
|
-
CursorConverter.of(cursorConverter(handle, path)),
|
|
89
|
-
semaphoreFacet.of(semaphore),
|
|
90
|
-
stateField,
|
|
91
|
-
viewPlugin,
|
|
92
|
-
],
|
|
93
|
-
};
|
|
100
|
+
return effects;
|
|
101
|
+
}),
|
|
102
|
+
];
|
|
94
103
|
};
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
import get from 'lodash.get';
|
|
6
6
|
|
|
7
7
|
import type { Prop } from '@dxos/automerge/automerge';
|
|
8
|
-
import { next as
|
|
8
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
9
9
|
|
|
10
10
|
import { type IDocHandle } from './defs';
|
|
11
|
-
import { type CursorConverter } from '
|
|
11
|
+
import { type CursorConverter } from '../cursor';
|
|
12
12
|
|
|
13
13
|
export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConverter => ({
|
|
14
14
|
// TODO(burdon): Handle assoc to associate with a previous character.
|
|
@@ -24,8 +24,9 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// NOTE: Slice is needed because getCursor mutates the array.
|
|
27
|
-
return
|
|
27
|
+
return A.getCursor(doc, path.slice(), pos);
|
|
28
28
|
},
|
|
29
|
+
|
|
29
30
|
fromCursor: (cursor) => {
|
|
30
31
|
if (cursor === '') {
|
|
31
32
|
return 0;
|
|
@@ -46,6 +47,6 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
// NOTE: Slice is needed because getCursor mutates the array.
|
|
49
|
-
return
|
|
50
|
+
return A.getCursorPosition(doc, path.slice(), cursor);
|
|
50
51
|
},
|
|
51
52
|
});
|
|
@@ -1,55 +1,34 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
3
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
4
5
|
//
|
|
5
6
|
|
|
6
|
-
import {
|
|
7
|
-
Annotation,
|
|
8
|
-
StateEffect,
|
|
9
|
-
type StateField,
|
|
10
|
-
type EditorState,
|
|
11
|
-
type Transaction,
|
|
12
|
-
type TransactionSpec,
|
|
13
|
-
} from '@codemirror/state';
|
|
7
|
+
import { Annotation, StateEffect, type StateField, type EditorState, type Transaction } from '@codemirror/state';
|
|
14
8
|
|
|
15
9
|
import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
16
10
|
|
|
17
|
-
export type
|
|
18
|
-
lastHeads: Heads;
|
|
11
|
+
export type State = {
|
|
19
12
|
path: Prop[];
|
|
13
|
+
lastHeads: Heads;
|
|
20
14
|
unreconciledTransactions: Transaction[];
|
|
21
15
|
};
|
|
22
16
|
|
|
23
|
-
|
|
17
|
+
export const getPath = (state: EditorState, field: StateField<State>): Prop[] => state.field(field).path;
|
|
18
|
+
export const getLastHeads = (state: EditorState, field: StateField<State>): Heads => state.field(field).lastHeads;
|
|
19
|
+
|
|
20
|
+
export type UpdateHeads = {
|
|
24
21
|
newHeads: Heads;
|
|
25
22
|
};
|
|
26
23
|
|
|
27
|
-
export const
|
|
28
|
-
|
|
29
|
-
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => effectType.of({ newHeads });
|
|
30
|
-
|
|
31
|
-
export const getLastHeads = (state: EditorState, field: StateField<Value>): Heads => state.field(field).lastHeads;
|
|
32
|
-
|
|
33
|
-
export const getPath = (state: EditorState, field: StateField<Value>): Prop[] => state.field(field).path;
|
|
24
|
+
export const updateHeadsEffect = StateEffect.define<UpdateHeads>({});
|
|
34
25
|
|
|
35
|
-
export const
|
|
26
|
+
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => updateHeadsEffect.of({ newHeads });
|
|
36
27
|
|
|
37
|
-
export const
|
|
28
|
+
export const reconcileAnnotation = Annotation.define<boolean>();
|
|
38
29
|
|
|
39
|
-
export const
|
|
40
|
-
|
|
41
|
-
if (tr.annotations instanceof Array) {
|
|
42
|
-
tr.annotations = [...tr.annotations, reconcileAnnotationType.of({})];
|
|
43
|
-
} else {
|
|
44
|
-
tr.annotations = [tr.annotations, reconcileAnnotationType.of({})];
|
|
45
|
-
}
|
|
46
|
-
} else {
|
|
47
|
-
tr.annotations = [reconcileAnnotationType.of({})];
|
|
48
|
-
}
|
|
49
|
-
// return {
|
|
50
|
-
// ...tr,
|
|
51
|
-
// annotations: reconcileAnnotationType.of({})
|
|
52
|
-
// }
|
|
30
|
+
export const isReconcile = (tr: Transaction): boolean => {
|
|
31
|
+
return !!tr.annotation(reconcileAnnotation);
|
|
53
32
|
};
|
|
54
33
|
|
|
55
34
|
export type IDocHandle<T = any> = {
|