@g9tm/lynxcontrols-richtext 0.0.0-stage → 2.5.9
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/README.md +32 -2
- package/catalog/cards/G9RichLines.md +16 -0
- package/catalog/cards/G9RichText.md +17 -0
- package/catalog/cards/G9RichTextEditor.md +17 -0
- package/catalog/catalog.json +1667 -0
- package/catalog/index.md +7 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/bidi.d.ts +24 -0
- package/dist/bidi.d.ts.map +1 -0
- package/dist/bidi.jsx +102 -0
- package/dist/editor.d.ts +111 -0
- package/dist/editor.d.ts.map +1 -0
- package/dist/editor.jsx +556 -0
- package/dist/element-core.d.ts +60 -0
- package/dist/element-core.d.ts.map +1 -0
- package/dist/element-core.jsx +173 -0
- package/dist/element-types.d.ts +31 -0
- package/dist/element-types.d.ts.map +1 -0
- package/dist/element-types.jsx +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +8 -0
- package/dist/lines-view.d.ts +27 -0
- package/dist/lines-view.d.ts.map +1 -0
- package/dist/lines-view.jsx +100 -0
- package/dist/mentions.d.ts +30 -0
- package/dist/mentions.d.ts.map +1 -0
- package/dist/mentions.jsx +40 -0
- package/dist/ot.d.ts +49 -0
- package/dist/ot.d.ts.map +1 -0
- package/dist/ot.jsx +306 -0
- package/dist/protocol.d.ts +199 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.jsx +11 -0
- package/dist/rich-text.d.ts +48 -0
- package/dist/rich-text.d.ts.map +1 -0
- package/dist/rich-text.jsx +186 -0
- package/dist/richtext.css +411 -0
- package/dist/richtext.css.map +1 -0
- package/dist/session.d.ts +142 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.jsx +665 -0
- package/dist/yjs.d.ts +3 -0
- package/dist/yjs.d.ts.map +1 -0
- package/dist/yjs.jsx +2 -0
- package/dist/ymodel.d.ts +178 -0
- package/dist/ymodel.d.ts.map +1 -0
- package/dist/ymodel.jsx +1379 -0
- package/package.json +68 -4
package/dist/editor.jsx
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
import { g9IdProps } from "@g9tm/lynxcontrols/base";
|
|
2
|
+
import { G9Avatar } from "@g9tm/lynxcontrols/display";
|
|
3
|
+
import { G9Menu } from "@g9tm/lynxcontrols/popover";
|
|
4
|
+
import { useG9Direction, useG9Layout } from "@g9tm/lynxcontrols/provider";
|
|
5
|
+
import { useEffect, useMemo, useRef, useState } from "@lynx-js/react";
|
|
6
|
+
import { nextDirection } from "./bidi.jsx";
|
|
7
|
+
import { G9RichText, G9RichTextPort } from "./rich-text.jsx";
|
|
8
|
+
import "./richtext.css";
|
|
9
|
+
const EN = {
|
|
10
|
+
undo: 'Undo',
|
|
11
|
+
redo: 'Redo',
|
|
12
|
+
style: 'Style',
|
|
13
|
+
text: 'Text',
|
|
14
|
+
h1: 'Heading 1',
|
|
15
|
+
h2: 'Heading 2',
|
|
16
|
+
h3: 'Heading 3',
|
|
17
|
+
bold: 'Bold',
|
|
18
|
+
italic: 'Italic',
|
|
19
|
+
underline: 'Underline',
|
|
20
|
+
strike: 'Strikethrough',
|
|
21
|
+
code: 'Code',
|
|
22
|
+
link: 'Link',
|
|
23
|
+
unlink: 'Remove link',
|
|
24
|
+
bullet: 'Bulleted list',
|
|
25
|
+
numbered: 'Numbered list',
|
|
26
|
+
checklist: 'Checklist',
|
|
27
|
+
indent: 'Indent',
|
|
28
|
+
outdent: 'Outdent',
|
|
29
|
+
quote: 'Quote',
|
|
30
|
+
callout: 'Callout',
|
|
31
|
+
callouts: {
|
|
32
|
+
info: 'Info',
|
|
33
|
+
success: 'Success',
|
|
34
|
+
warning: 'Warning',
|
|
35
|
+
danger: 'Danger'
|
|
36
|
+
},
|
|
37
|
+
directionAuto: 'Direction: automatic',
|
|
38
|
+
directionRtl: 'Right to left',
|
|
39
|
+
directionLtr: 'Left to right',
|
|
40
|
+
insert: 'Insert',
|
|
41
|
+
table: 'Table',
|
|
42
|
+
image: 'Picture',
|
|
43
|
+
codeBlock: 'Code block',
|
|
44
|
+
divider: 'Divider',
|
|
45
|
+
date: 'Date',
|
|
46
|
+
mention: 'Mention',
|
|
47
|
+
linkTo: 'Link to…',
|
|
48
|
+
close: 'Close'
|
|
49
|
+
};
|
|
50
|
+
function useG9RichTextSession(create, deps) {
|
|
51
|
+
const port = useMemo(()=>new G9RichTextPort(), []);
|
|
52
|
+
const [session, setSession] = useState(null);
|
|
53
|
+
const [version, setVersion] = useState(0);
|
|
54
|
+
const timer = useRef(null);
|
|
55
|
+
const onState = useMemo(()=>()=>{
|
|
56
|
+
if (timer.current) return;
|
|
57
|
+
timer.current = setTimeout(()=>{
|
|
58
|
+
timer.current = null;
|
|
59
|
+
setVersion((v)=>v + 1);
|
|
60
|
+
}, 30);
|
|
61
|
+
}, []);
|
|
62
|
+
useEffect(()=>{
|
|
63
|
+
const s = create(port, onState);
|
|
64
|
+
setSession(s);
|
|
65
|
+
return ()=>{
|
|
66
|
+
var _s_destroy;
|
|
67
|
+
if (timer.current) clearTimeout(timer.current);
|
|
68
|
+
timer.current = null;
|
|
69
|
+
null == (_s_destroy = s.destroy) || _s_destroy.call(s);
|
|
70
|
+
setSession(null);
|
|
71
|
+
};
|
|
72
|
+
}, deps);
|
|
73
|
+
return {
|
|
74
|
+
session,
|
|
75
|
+
port,
|
|
76
|
+
version,
|
|
77
|
+
onState
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const isoToday = ()=>{
|
|
81
|
+
const d = new Date();
|
|
82
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
83
|
+
};
|
|
84
|
+
function G9RichTextEditor(props) {
|
|
85
|
+
var _props_today, _props_toolbar;
|
|
86
|
+
var _props_labels;
|
|
87
|
+
const { wide } = useG9Layout();
|
|
88
|
+
const rtl = 'rtl' === useG9Direction();
|
|
89
|
+
const L = {
|
|
90
|
+
...EN,
|
|
91
|
+
...props.labels,
|
|
92
|
+
callouts: {
|
|
93
|
+
...EN.callouts,
|
|
94
|
+
...null == (_props_labels = props.labels) ? void 0 : _props_labels.callouts
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const session = props.session;
|
|
98
|
+
const [focused, setFocused] = useState(false);
|
|
99
|
+
const [found, setFound] = useState([]);
|
|
100
|
+
const [dismissed, setDismissed] = useState(null);
|
|
101
|
+
const today = null != (_props_today = props.today) ? _props_today : isoToday;
|
|
102
|
+
const trigger = useMemo(()=>{
|
|
103
|
+
if (!session || !props.editable || !focused) return null;
|
|
104
|
+
const { a, h } = session.sel;
|
|
105
|
+
if (a !== h) return null;
|
|
106
|
+
const text1 = session.plainText;
|
|
107
|
+
const before = text1.slice(text1.lastIndexOf('\n', h - 1) + 1, h);
|
|
108
|
+
if ('code' === session.state().block) return null;
|
|
109
|
+
let m = /\[\[([^\]\n]{0,40})$/.exec(before);
|
|
110
|
+
if (m && props.links) return {
|
|
111
|
+
kind: 'link',
|
|
112
|
+
from: h - m[0].length,
|
|
113
|
+
to: h,
|
|
114
|
+
query: m[1]
|
|
115
|
+
};
|
|
116
|
+
m = /(?:^|[\s(])@([^@\n]{0,30})$/.exec(before);
|
|
117
|
+
if (m && props.people && !/\s\s$/.test(m[1])) return {
|
|
118
|
+
kind: 'mention',
|
|
119
|
+
from: h - m[1].length - 1,
|
|
120
|
+
to: h,
|
|
121
|
+
query: m[1]
|
|
122
|
+
};
|
|
123
|
+
m = /(?:^|\s)\/([^\s/]{0,20})$/.exec(before);
|
|
124
|
+
if (m) return {
|
|
125
|
+
kind: 'slash',
|
|
126
|
+
from: h - m[1].length - 1,
|
|
127
|
+
to: h,
|
|
128
|
+
query: m[1]
|
|
129
|
+
};
|
|
130
|
+
return null;
|
|
131
|
+
}, [
|
|
132
|
+
props.version,
|
|
133
|
+
focused,
|
|
134
|
+
props.editable,
|
|
135
|
+
session
|
|
136
|
+
]);
|
|
137
|
+
const triggerKey = trigger ? `${trigger.kind}:${trigger.from}` : null;
|
|
138
|
+
const range = ()=>{
|
|
139
|
+
const { a, h } = session.sel;
|
|
140
|
+
return [
|
|
141
|
+
Math.min(a, h),
|
|
142
|
+
Math.max(a, h)
|
|
143
|
+
];
|
|
144
|
+
};
|
|
145
|
+
const run = (fn)=>()=>{
|
|
146
|
+
const [from, to] = range();
|
|
147
|
+
null == session || session.command(()=>fn(from, to));
|
|
148
|
+
};
|
|
149
|
+
const insertPicture = async ()=>{
|
|
150
|
+
var _props_uploadImage;
|
|
151
|
+
const picked = await (null == (_props_uploadImage = props.uploadImage) ? void 0 : _props_uploadImage.call(props));
|
|
152
|
+
if (picked && session) session.insertBlock('image', {
|
|
153
|
+
key: picked.key,
|
|
154
|
+
alt: picked.alt
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
const slashItems = (at)=>{
|
|
158
|
+
const s = session;
|
|
159
|
+
const remove = ()=>s.replaceText(at.from, at.to, '');
|
|
160
|
+
const block = (fn)=>()=>{
|
|
161
|
+
remove();
|
|
162
|
+
const [from, to] = range();
|
|
163
|
+
s.command(()=>fn(from, to));
|
|
164
|
+
};
|
|
165
|
+
const all = [
|
|
166
|
+
{
|
|
167
|
+
id: 'text',
|
|
168
|
+
label: L.text,
|
|
169
|
+
glyph: '¶',
|
|
170
|
+
keywords: 'text paragraph متن',
|
|
171
|
+
run: block((f, t)=>s.model.setBlockType(f, t, 'p'))
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
id: 'h1',
|
|
175
|
+
label: L.h1,
|
|
176
|
+
glyph: 'H1',
|
|
177
|
+
keywords: 'h1 heading title عنوان',
|
|
178
|
+
run: block((f, t)=>s.model.setBlockType(f, t, 'h1'))
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: 'h2',
|
|
182
|
+
label: L.h2,
|
|
183
|
+
glyph: 'H2',
|
|
184
|
+
keywords: 'h2 heading عنوان',
|
|
185
|
+
run: block((f, t)=>s.model.setBlockType(f, t, 'h2'))
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
id: 'h3',
|
|
189
|
+
label: L.h3,
|
|
190
|
+
glyph: 'H3',
|
|
191
|
+
keywords: 'h3 heading عنوان',
|
|
192
|
+
run: block((f, t)=>s.model.setBlockType(f, t, 'h3'))
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
id: 'bullet',
|
|
196
|
+
label: L.bullet,
|
|
197
|
+
glyph: '•',
|
|
198
|
+
keywords: 'bullet list ul فهرست',
|
|
199
|
+
run: block((f, t)=>s.model.toggleList(f, t, 'bullet'))
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'numbered',
|
|
203
|
+
label: L.numbered,
|
|
204
|
+
glyph: '1.',
|
|
205
|
+
keywords: 'numbered ordered list شماره',
|
|
206
|
+
run: block((f, t)=>s.model.toggleList(f, t, 'ordered'))
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
id: 'checklist',
|
|
210
|
+
label: L.checklist,
|
|
211
|
+
glyph: '☑',
|
|
212
|
+
keywords: 'todo task check کار چک',
|
|
213
|
+
run: block((f, t)=>s.model.toggleList(f, t, 'task'))
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
id: 'quote',
|
|
217
|
+
label: L.quote,
|
|
218
|
+
glyph: '❝',
|
|
219
|
+
keywords: 'quote نقل',
|
|
220
|
+
run: block((f, t)=>s.model.toggleQuote(f, t))
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
id: 'callout',
|
|
224
|
+
label: L.callout,
|
|
225
|
+
glyph: 'ⓘ',
|
|
226
|
+
keywords: 'callout note info نکته',
|
|
227
|
+
run: block((f, t)=>s.model.toggleCallout(f, t, 'info'))
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
id: 'code',
|
|
231
|
+
label: L.codeBlock,
|
|
232
|
+
glyph: '</>',
|
|
233
|
+
keywords: 'code pre کد',
|
|
234
|
+
run: block((f, t)=>s.model.setBlockType(f, t, 'code'))
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: 'table',
|
|
238
|
+
label: L.table,
|
|
239
|
+
glyph: '▦',
|
|
240
|
+
keywords: 'table grid جدول',
|
|
241
|
+
run: ()=>(remove(), s.insertBlock('table', {
|
|
242
|
+
rows: 3,
|
|
243
|
+
cols: 3,
|
|
244
|
+
header: true
|
|
245
|
+
}))
|
|
246
|
+
},
|
|
247
|
+
...props.uploadImage ? [
|
|
248
|
+
{
|
|
249
|
+
id: 'image',
|
|
250
|
+
label: L.image,
|
|
251
|
+
glyph: '🖼',
|
|
252
|
+
keywords: 'image picture photo تصویر عکس',
|
|
253
|
+
run: ()=>(remove(), void insertPicture())
|
|
254
|
+
}
|
|
255
|
+
] : [],
|
|
256
|
+
{
|
|
257
|
+
id: 'divider',
|
|
258
|
+
label: L.divider,
|
|
259
|
+
glyph: '—',
|
|
260
|
+
keywords: 'divider hr line خط',
|
|
261
|
+
run: ()=>(remove(), s.insertBlock('hr'))
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
id: 'date',
|
|
265
|
+
label: L.date,
|
|
266
|
+
glyph: '📅',
|
|
267
|
+
keywords: 'date today تاریخ امروز',
|
|
268
|
+
run: ()=>s.insertInline('dateChip', {
|
|
269
|
+
date: today()
|
|
270
|
+
}, {
|
|
271
|
+
from: at.from,
|
|
272
|
+
to: at.to
|
|
273
|
+
})
|
|
274
|
+
},
|
|
275
|
+
...props.people ? [
|
|
276
|
+
{
|
|
277
|
+
id: 'mention',
|
|
278
|
+
label: L.mention,
|
|
279
|
+
glyph: '@',
|
|
280
|
+
keywords: 'mention person اشاره',
|
|
281
|
+
run: ()=>s.replaceText(at.from, at.to, '@')
|
|
282
|
+
}
|
|
283
|
+
] : [],
|
|
284
|
+
...props.links ? [
|
|
285
|
+
{
|
|
286
|
+
id: 'link',
|
|
287
|
+
label: L.linkTo,
|
|
288
|
+
glyph: '🔗',
|
|
289
|
+
keywords: 'link task doc پیوند',
|
|
290
|
+
run: ()=>s.replaceText(at.from, at.to, '[[')
|
|
291
|
+
}
|
|
292
|
+
] : []
|
|
293
|
+
];
|
|
294
|
+
const q = at.query.trim().toLowerCase();
|
|
295
|
+
return all.filter((i)=>!q || i.label.toLowerCase().includes(q) || i.keywords.includes(q)).slice(0, 12);
|
|
296
|
+
};
|
|
297
|
+
useEffect(()=>{
|
|
298
|
+
if (!session || !trigger || dismissed === triggerKey) return void setFound([]);
|
|
299
|
+
let cancelled = false;
|
|
300
|
+
const go = async ()=>{
|
|
301
|
+
if ('slash' === trigger.kind) return void setFound(slashItems(trigger));
|
|
302
|
+
if ('mention' === trigger.kind) {
|
|
303
|
+
const list = await props.people(trigger.query).catch(()=>[]);
|
|
304
|
+
if (!cancelled) setFound(list.slice(0, 8).map((p)=>({
|
|
305
|
+
id: p.id,
|
|
306
|
+
label: p.name,
|
|
307
|
+
person: p,
|
|
308
|
+
run: ()=>session.insertInline('mention', {
|
|
309
|
+
id: p.id,
|
|
310
|
+
label: p.name,
|
|
311
|
+
mentionSuggestionChar: '@'
|
|
312
|
+
}, {
|
|
313
|
+
from: trigger.from,
|
|
314
|
+
to: trigger.to
|
|
315
|
+
})
|
|
316
|
+
})));
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const options = await props.links(trigger.query).catch(()=>[]);
|
|
320
|
+
if (!cancelled) setFound(options.slice(0, 8).map((o)=>({
|
|
321
|
+
id: o.id,
|
|
322
|
+
label: o.label,
|
|
323
|
+
...o.sub ? {
|
|
324
|
+
sub: o.sub
|
|
325
|
+
} : {},
|
|
326
|
+
glyph: '🔗',
|
|
327
|
+
run: ()=>session.insertInline('refChip', {
|
|
328
|
+
href: o.href,
|
|
329
|
+
kind: o.kind
|
|
330
|
+
}, {
|
|
331
|
+
from: trigger.from,
|
|
332
|
+
to: trigger.to
|
|
333
|
+
})
|
|
334
|
+
})));
|
|
335
|
+
};
|
|
336
|
+
const timer = setTimeout(()=>void go(), 'slash' === trigger.kind ? 0 : 150);
|
|
337
|
+
return ()=>{
|
|
338
|
+
cancelled = true;
|
|
339
|
+
clearTimeout(timer);
|
|
340
|
+
};
|
|
341
|
+
}, [
|
|
342
|
+
triggerKey,
|
|
343
|
+
null == trigger ? void 0 : trigger.query,
|
|
344
|
+
dismissed
|
|
345
|
+
]);
|
|
346
|
+
const state = props.editable && session ? session.state() : null;
|
|
347
|
+
const placement = null != (_props_toolbar = props.toolbar) ? _props_toolbar : 'auto';
|
|
348
|
+
const bottom = 'bottom' === placement || 'auto' === placement && !wide;
|
|
349
|
+
const showToolbar = 'none' !== placement && state && session && (!bottom || 'bottom' === placement || focused);
|
|
350
|
+
const toolbar = showToolbar && state && session ? <Toolbar {...props.id ? {
|
|
351
|
+
id: `${props.id}_Toolbar`
|
|
352
|
+
} : {}} session={session} state={state} labels={L} rtl={rtl} run={run} today={today} {...props.uploadImage ? {
|
|
353
|
+
onPicture: ()=>void insertPicture()
|
|
354
|
+
} : {}} {...props.promptLink ? {
|
|
355
|
+
promptLink: props.promptLink
|
|
356
|
+
} : {}} mentions={void 0 !== props.people} links={void 0 !== props.links}/> : null;
|
|
357
|
+
const suggestions = found.length > 0 && trigger && dismissed !== triggerKey ? <scroll-view className="g9-rt-suggest" scroll-orientation="vertical">
|
|
358
|
+
{found.map((item)=>{
|
|
359
|
+
var _item_person_image, _item_glyph;
|
|
360
|
+
return <view key={item.id} className="g9-rt-suggest__row" {...g9IdProps(props.id ? `${props.id}_Suggest_${item.id}` : void 0)} style={{
|
|
361
|
+
flexDirection: rtl ? 'row-reverse' : 'row',
|
|
362
|
+
direction: 'ltr'
|
|
363
|
+
}} bindtap={()=>item.run()} accessibility-element={true} accessibility-traits="button" accessibility-label={item.sub ? `${item.label}, ${item.sub}` : item.label}>
|
|
364
|
+
{item.person ? <G9Avatar name={item.person.name} image={null != (_item_person_image = item.person.image) ? _item_person_image : null} size={24}/> : <text className="g9-rt-suggest__glyph">{null != (_item_glyph = item.glyph) ? _item_glyph : ''}</text>}
|
|
365
|
+
<view className="g9-rt-suggest__texts">
|
|
366
|
+
<text className="g9-rt-suggest__label" text-maxline="1" style={{
|
|
367
|
+
textAlign: rtl ? 'right' : 'left'
|
|
368
|
+
}}>
|
|
369
|
+
{item.label}
|
|
370
|
+
</text>
|
|
371
|
+
{item.sub ? <text className="g9-rt-suggest__sub" text-maxline="1" style={{
|
|
372
|
+
textAlign: rtl ? 'right' : 'left'
|
|
373
|
+
}}>
|
|
374
|
+
{item.sub}
|
|
375
|
+
</text> : null}
|
|
376
|
+
</view>
|
|
377
|
+
</view>;
|
|
378
|
+
})}
|
|
379
|
+
<view className="g9-rt-suggest__row" {...g9IdProps(props.id ? `${props.id}_Suggest_Close` : void 0)} bindtap={()=>setDismissed(triggerKey)} accessibility-element={true} accessibility-traits="button" accessibility-label={L.close}>
|
|
380
|
+
<text className="g9-rt-suggest__sub">{`✕ ${L.close}`}</text>
|
|
381
|
+
</view>
|
|
382
|
+
</scroll-view> : null;
|
|
383
|
+
if (!session) return <view className="g9-rt-editor"/>;
|
|
384
|
+
return <view className={`g9-rt-editor${props.className ? ` ${props.className}` : ''}`} {...g9IdProps(props.id)}>
|
|
385
|
+
{!bottom && toolbar ? <view className="g9-rt-toolbar g9-rt-toolbar--top">{toolbar}</view> : null}
|
|
386
|
+
<G9RichText session={session} port={props.port} editable={props.editable} placeholder={props.placeholder} className="g9-rt-editor__surface" {...props.surface ? {
|
|
387
|
+
surface: props.surface
|
|
388
|
+
} : {}} {...props.palette ? {
|
|
389
|
+
palette: props.palette
|
|
390
|
+
} : {}} onFocusChange={setFocused} onTapObject={(pos)=>{
|
|
391
|
+
var _props_onOpenLink;
|
|
392
|
+
const inline = session.model.inlineAt(pos);
|
|
393
|
+
if ((null == inline ? void 0 : inline.name) === 'refChip' && 'string' == typeof inline.attrs.href) null == (_props_onOpenLink = props.onOpenLink) || _props_onOpenLink.call(props, inline.attrs.href);
|
|
394
|
+
}}/>
|
|
395
|
+
{suggestions}
|
|
396
|
+
{bottom && toolbar ? <view className="g9-rt-toolbar g9-rt-toolbar--bottom">{toolbar}</view> : props.footer}
|
|
397
|
+
</view>;
|
|
398
|
+
}
|
|
399
|
+
function Tool(props) {
|
|
400
|
+
return <view className={`g9-rt-tool${props.active ? ' g9-rt-tool--on' : ''}${props.disabled ? ' g9-rt-tool--off' : ''}`} {...g9IdProps(props.id)} {...props.disabled ? {} : {
|
|
401
|
+
bindtap: props.onPress
|
|
402
|
+
}} accessibility-element={true} accessibility-traits="button" accessibility-label={props.label} {...props.active ? {
|
|
403
|
+
'accessibility-value': '✓'
|
|
404
|
+
} : {}}>
|
|
405
|
+
<text className={`g9-rt-tool__glyph${props.className ? ` ${props.className}` : ''}`}>{props.glyph}</text>
|
|
406
|
+
</view>;
|
|
407
|
+
}
|
|
408
|
+
function Toolbar(props) {
|
|
409
|
+
const { session, state, run } = props;
|
|
410
|
+
const L = props.labels;
|
|
411
|
+
const [menu, setMenu] = useState(null);
|
|
412
|
+
const id = (s)=>props.id ? `${props.id}_${s}` : void 0;
|
|
413
|
+
const has = (mark)=>mark in state.marks;
|
|
414
|
+
const blockLabel = 'h1' === state.block ? L.h1 : 'h2' === state.block ? L.h2 : 'h3' === state.block ? L.h3 : L.text;
|
|
415
|
+
const link = async ()=>{
|
|
416
|
+
var _this, _props_promptLink;
|
|
417
|
+
if (has('link')) return void session.setLink(null);
|
|
418
|
+
const href = null == (_this = await (null == (_props_promptLink = props.promptLink) ? void 0 : _props_promptLink.call(props))) ? void 0 : _this.trim();
|
|
419
|
+
if (!href) return;
|
|
420
|
+
const clean = /^[a-z][a-z0-9+.-]*:/i.test(href) ? href : `https://${href}`;
|
|
421
|
+
if (/^(https?|mailto|tel):/i.test(clean)) session.setLink(clean);
|
|
422
|
+
};
|
|
423
|
+
const styleItems = [
|
|
424
|
+
'p',
|
|
425
|
+
'h1',
|
|
426
|
+
'h2',
|
|
427
|
+
'h3'
|
|
428
|
+
].map((b)=>({
|
|
429
|
+
key: b,
|
|
430
|
+
label: 'p' === b ? L.text : L[b],
|
|
431
|
+
checked: state.block === b
|
|
432
|
+
}));
|
|
433
|
+
const insertItems = [
|
|
434
|
+
{
|
|
435
|
+
key: 'table',
|
|
436
|
+
label: L.table
|
|
437
|
+
},
|
|
438
|
+
...props.onPicture ? [
|
|
439
|
+
{
|
|
440
|
+
key: 'image',
|
|
441
|
+
label: L.image
|
|
442
|
+
}
|
|
443
|
+
] : [],
|
|
444
|
+
{
|
|
445
|
+
key: 'code',
|
|
446
|
+
label: L.codeBlock
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
key: 'divider',
|
|
450
|
+
label: L.divider
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
key: 'date',
|
|
454
|
+
label: L.date
|
|
455
|
+
},
|
|
456
|
+
...props.mentions ? [
|
|
457
|
+
{
|
|
458
|
+
key: 'mention',
|
|
459
|
+
label: L.mention,
|
|
460
|
+
separatorBefore: true
|
|
461
|
+
}
|
|
462
|
+
] : [],
|
|
463
|
+
...props.links ? [
|
|
464
|
+
{
|
|
465
|
+
key: 'link',
|
|
466
|
+
label: L.linkTo,
|
|
467
|
+
...props.mentions ? {} : {
|
|
468
|
+
separatorBefore: true
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
] : []
|
|
472
|
+
];
|
|
473
|
+
const toneItems = [
|
|
474
|
+
'info',
|
|
475
|
+
'success',
|
|
476
|
+
'warning',
|
|
477
|
+
'danger'
|
|
478
|
+
].map((t)=>({
|
|
479
|
+
key: t,
|
|
480
|
+
label: L.callouts[t],
|
|
481
|
+
checked: state.callout === t
|
|
482
|
+
}));
|
|
483
|
+
const insert = (key)=>{
|
|
484
|
+
var _props_onPicture;
|
|
485
|
+
const pos = ()=>{
|
|
486
|
+
const { a, h } = session.sel;
|
|
487
|
+
return [
|
|
488
|
+
Math.min(a, h),
|
|
489
|
+
Math.max(a, h)
|
|
490
|
+
];
|
|
491
|
+
};
|
|
492
|
+
if ('table' === key) session.insertBlock('table', {
|
|
493
|
+
rows: 3,
|
|
494
|
+
cols: 3,
|
|
495
|
+
header: true
|
|
496
|
+
});
|
|
497
|
+
else if ('image' === key) null == (_props_onPicture = props.onPicture) || _props_onPicture.call(props);
|
|
498
|
+
else if ('code' === key) run((f, t)=>session.model.setBlockType(f, t, 'code'))();
|
|
499
|
+
else if ('divider' === key) session.insertBlock('hr');
|
|
500
|
+
else if ('date' === key) session.insertInline('dateChip', {
|
|
501
|
+
date: props.today()
|
|
502
|
+
});
|
|
503
|
+
else {
|
|
504
|
+
const [from, to] = pos();
|
|
505
|
+
session.replaceText(from, to, 'mention' === key ? ' @' : ' [[');
|
|
506
|
+
session.focus();
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
const dirGlyph = 'rtl' === state.dir ? '⇠¶' : 'ltr' === state.dir ? '¶⇢' : '⇄';
|
|
510
|
+
const dirLabel = 'rtl' === state.dir ? L.directionRtl : 'ltr' === state.dir ? L.directionLtr : L.directionAuto;
|
|
511
|
+
return <view>
|
|
512
|
+
<scroll-view scroll-orientation="horizontal" className="g9-rt-toolbar__scroll">
|
|
513
|
+
<view className="g9-rt-toolbar__row" style={{
|
|
514
|
+
flexDirection: props.rtl ? 'row-reverse' : 'row',
|
|
515
|
+
direction: 'ltr'
|
|
516
|
+
}}>
|
|
517
|
+
<Tool id={id('Undo')} glyph={props.rtl ? '↷' : '↶'} label={L.undo} disabled={!session.canUndo} onPress={()=>session.undo()}/>
|
|
518
|
+
<Tool id={id('Redo')} glyph={props.rtl ? '↶' : '↷'} label={L.redo} disabled={!session.canRedo} onPress={()=>session.redo()}/>
|
|
519
|
+
<view className="g9-rt-toolbar__sep"/>
|
|
520
|
+
<view className="g9-rt-toolbar__style" {...g9IdProps(id('Style'))} bindtap={()=>setMenu('style')} accessibility-element={true} accessibility-traits="button" accessibility-label={`${L.style}: ${blockLabel}`}>
|
|
521
|
+
<text className="g9-rt-toolbar__style-text">{`${blockLabel} ▾`}</text>
|
|
522
|
+
</view>
|
|
523
|
+
<view className="g9-rt-toolbar__sep"/>
|
|
524
|
+
<Tool id={id('Bold')} glyph="B" className="g9-rt-tool__glyph--b" label={L.bold} active={has('bold')} onPress={()=>session.toggleMark('bold')}/>
|
|
525
|
+
<Tool id={id('Italic')} glyph="I" className="g9-rt-tool__glyph--i" label={L.italic} active={has('italic')} onPress={()=>session.toggleMark('italic')}/>
|
|
526
|
+
<Tool id={id('Underline')} glyph="U" className="g9-rt-tool__glyph--u" label={L.underline} active={has('underline')} onPress={()=>session.toggleMark('underline')}/>
|
|
527
|
+
<Tool id={id('Strike')} glyph="S" className="g9-rt-tool__glyph--s" label={L.strike} active={has('strike')} onPress={()=>session.toggleMark('strike')}/>
|
|
528
|
+
<Tool id={id('Code')} glyph="</>" label={L.code} active={has('code')} onPress={()=>session.toggleMark('code')}/>
|
|
529
|
+
{props.promptLink || has('link') ? <Tool id={id('Link')} glyph="🔗" label={has('link') ? L.unlink : L.link} active={has('link')} onPress={()=>void link()}/> : null}
|
|
530
|
+
<view className="g9-rt-toolbar__sep"/>
|
|
531
|
+
<Tool id={id('Bullet')} glyph="•" label={L.bullet} active={'bullet' === state.list} onPress={run((f, t)=>session.model.toggleList(f, t, 'bullet'))}/>
|
|
532
|
+
<Tool id={id('Numbered')} glyph="1." label={L.numbered} active={'ordered' === state.list} onPress={run((f, t)=>session.model.toggleList(f, t, 'ordered'))}/>
|
|
533
|
+
<Tool id={id('Checklist')} glyph="☑" label={L.checklist} active={'task' === state.list} onPress={run((f, t)=>session.model.toggleList(f, t, 'task'))}/>
|
|
534
|
+
{state.list ? <>
|
|
535
|
+
<Tool id={id('Outdent')} glyph={props.rtl ? '⇥' : '⇤'} label={L.outdent} disabled={!state.canOutdent} onPress={run((f, t)=>session.model.outdent(f, t))}/>
|
|
536
|
+
<Tool id={id('Indent')} glyph={props.rtl ? '⇤' : '⇥'} label={L.indent} disabled={!state.canIndent} onPress={run((f, t)=>session.model.indent(f, t))}/>
|
|
537
|
+
</> : null}
|
|
538
|
+
<Tool id={id('Quote')} glyph="❝" label={L.quote} active={state.quote} onPress={run((f, t)=>session.model.toggleQuote(f, t))}/>
|
|
539
|
+
<Tool id={id('Callout')} glyph="ⓘ" label={L.callout} active={Boolean(state.callout)} onPress={()=>state.callout ? setMenu('tone') : run((f, t)=>session.model.toggleCallout(f, t, 'info'))()}/>
|
|
540
|
+
<Tool id={id('Direction')} glyph={dirGlyph} label={dirLabel} active={null !== state.dir} onPress={run((f, t)=>session.model.setDirection(f, t, nextDirection(state.dir)))}/>
|
|
541
|
+
<view className="g9-rt-toolbar__sep"/>
|
|
542
|
+
<Tool id={id('Insert')} glyph="+" label={L.insert} onPress={()=>setMenu('insert')}/>
|
|
543
|
+
</view>
|
|
544
|
+
</scroll-view>
|
|
545
|
+
<G9Menu open={null !== menu} onClose={()=>setMenu(null)} {...props.id ? {
|
|
546
|
+
id: `${props.id}_Menu`
|
|
547
|
+
} : {}} {...id('style' === menu ? 'Style' : 'tone' === menu ? 'Callout' : 'Insert') ? {
|
|
548
|
+
anchorId: id('style' === menu ? 'Style' : 'tone' === menu ? 'Callout' : 'Insert')
|
|
549
|
+
} : {}} title={'style' === menu ? L.style : 'tone' === menu ? L.callout : L.insert} items={'style' === menu ? styleItems : 'tone' === menu ? toneItems : insertItems} onSelect={(key)=>{
|
|
550
|
+
if ('style' === menu) run((f, t)=>session.model.setBlockType(f, t, key))();
|
|
551
|
+
else if ('tone' === menu) session.command(()=>session.model.setCalloutTone(Math.max(session.sel.a, session.sel.h), key));
|
|
552
|
+
else insert(key);
|
|
553
|
+
}}/>
|
|
554
|
+
</view>;
|
|
555
|
+
}
|
|
556
|
+
export { G9RichTextEditor, useG9RichTextSession };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { RtEdit, RtLine, RtMessage, RtSelect } from './protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* The element's side of the `<g9-richtext>` protocol without any view: what every host's element does with its text,
|
|
4
|
+
* its lines and the app's messages (protocol.ts). The web host's element uses this class as is; the Android element
|
|
5
|
+
* (Kotlin) follows it line by line; tests drive it against a real session.
|
|
6
|
+
*
|
|
7
|
+
* - A local edit changes the text at once, gets the next `seq` and the `base` (the last applied message's `rev`), and
|
|
8
|
+
* the display lines follow the edit roughly (a new line copies its neighbour's style) until the app's answer.
|
|
9
|
+
* - A message is applied only when its `ack` equals the last `seq` (no edit on its way). Then the text gets the
|
|
10
|
+
* message's replacements, the app's lines replace the display lines, the selection follows the replacements (a
|
|
11
|
+
* caret stays before text inserted at it) unless the message moves it.
|
|
12
|
+
* - A line change the element cannot apply (it does not have the state it is based on): ask for `resync`.
|
|
13
|
+
*/
|
|
14
|
+
export interface Applied {
|
|
15
|
+
/** Replacements applied to the text, in order (positions after the previous ones). */
|
|
16
|
+
ops: {
|
|
17
|
+
from: number;
|
|
18
|
+
to: number;
|
|
19
|
+
text: string;
|
|
20
|
+
}[];
|
|
21
|
+
/** The text was replaced as a whole. */
|
|
22
|
+
full: boolean;
|
|
23
|
+
/** Lines `[from, to)` of the new line array changed style (the view restyles them). */
|
|
24
|
+
restyle: [number, number] | null;
|
|
25
|
+
/** The selection after the message. */
|
|
26
|
+
sel: {
|
|
27
|
+
a: number;
|
|
28
|
+
h: number;
|
|
29
|
+
};
|
|
30
|
+
/** The message asks for the keyboard. */
|
|
31
|
+
focus: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** The `<g9-richtext>` element's model shared by every host: lines, edits, the caret and the app's messages. */
|
|
34
|
+
export declare class ElementCore {
|
|
35
|
+
text: string;
|
|
36
|
+
/** The app's lines as of `rev` (the base of the next line change). */
|
|
37
|
+
appLines: RtLine[];
|
|
38
|
+
/** What is drawn: the app's lines, adjusted for edits the app has not answered yet. */
|
|
39
|
+
lines: RtLine[];
|
|
40
|
+
seq: number;
|
|
41
|
+
rev: number;
|
|
42
|
+
sel: {
|
|
43
|
+
a: number;
|
|
44
|
+
h: number;
|
|
45
|
+
};
|
|
46
|
+
focused: boolean;
|
|
47
|
+
/** The user replaced `from..to` with `text` (already done in the view): the event to send. */
|
|
48
|
+
edit(from: number, to: number, text: string): RtEdit;
|
|
49
|
+
/** The selection moved (or the focus changed): the event to send. */
|
|
50
|
+
select(a: number, h: number, focus?: boolean): RtSelect;
|
|
51
|
+
/** A message from the app: what changed, `null` when it waits for the next one, `'resync'` when it cannot be applied. */
|
|
52
|
+
apply(message: RtMessage): Applied | null | 'resync';
|
|
53
|
+
}
|
|
54
|
+
/** A position through a replacement; a caret stays before text inserted right at it. */
|
|
55
|
+
export declare function mapPos(pos: number, op: {
|
|
56
|
+
from: number;
|
|
57
|
+
to: number;
|
|
58
|
+
text: string;
|
|
59
|
+
}): number;
|
|
60
|
+
//# sourceMappingURL=element-core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element-core.d.ts","sourceRoot":"","sources":["../src/element-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtE;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,OAAO;IACtB,sFAAsF;IACtF,GAAG,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAClD,wCAAwC;IACxC,IAAI,EAAE,OAAO,CAAC;IACd,uFAAuF;IACvF,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;IACjC,uCAAuC;IACvC,GAAG,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9B,yCAAyC;IACzC,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,gHAAgH;AAChH,qBAAa,WAAW;IACtB,IAAI,SAAM;IACV,sEAAsE;IACtE,QAAQ,EAAE,MAAM,EAAE,CAAuB;IACzC,uFAAuF;IACvF,KAAK,EAAE,MAAM,EAAE,CAAuB;IACtC,GAAG,SAAK;IACR,GAAG,SAAK;IACR,GAAG;;;MAAkB;IACrB,OAAO,UAAS;IAEhB,8FAA8F;IAC9F,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAiBpD,qEAAqE;IACrE,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,UAAe,GAAG,QAAQ;IAM5D,yHAAyH;IACzH,KAAK,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,GAAG,IAAI,GAAG,QAAQ;CA2CrD;AAQD,wFAAwF;AACxF,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAI1F"}
|