@peers-app/peers-ui 0.33.21 → 0.33.23
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/command-palette/command-palette.js +3 -43
- package/dist/components/markdown-editor/autolink-plugin.d.ts +5 -1
- package/dist/components/markdown-editor/autolink-plugin.js +5 -8
- package/dist/components/markdown-editor/blocks.d.ts +83 -0
- package/dist/components/markdown-editor/blocks.js +201 -0
- package/dist/components/markdown-editor/code-plugin.d.ts +21 -0
- package/dist/components/markdown-editor/code-plugin.js +109 -0
- package/dist/components/markdown-editor/editor.d.ts +12 -5
- package/dist/components/markdown-editor/editor.js +27 -6
- package/dist/components/markdown-editor/link-plugin.d.ts +41 -0
- package/dist/components/markdown-editor/link-plugin.js +334 -0
- package/dist/components/markdown-editor/markdown-plugin.d.ts +23 -6
- package/dist/components/markdown-editor/markdown-plugin.js +94 -9
- package/dist/components/markdown-editor/mentions-plugin.d.ts +25 -0
- package/dist/components/markdown-editor/mentions-plugin.js +47 -22
- package/dist/components/markdown-editor/theme.d.ts +35 -0
- package/dist/components/markdown-editor/theme.js +36 -0
- package/dist/components/markdown-editor/toolbar.d.ts +11 -0
- package/dist/components/markdown-editor/toolbar.js +16 -5
- package/dist/components/qr-scanner.js +2 -34
- package/dist/theme.css +225 -4
- package/dist/ui-router/routes-loader.js +6 -9
- package/dist/ui-router/runnable-version.d.ts +9 -0
- package/dist/ui-router/runnable-version.js +20 -0
- package/dist/ui-router/ui-loader.js +2 -9
- package/package.json +10 -8
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type LinkNode } from "@lexical/link";
|
|
2
|
+
import { type LexicalCommand, type RangeSelection } from "lexical";
|
|
3
|
+
/**
|
|
4
|
+
* Whether `url` is a link the editor keeps: an `http:`, `https:` or `mailto:` URL.
|
|
5
|
+
*
|
|
6
|
+
* @param url - The text to check.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isLinkUrl(url: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* What a typed link becomes: the URL as given when it's a web or mail link, `https://` before a
|
|
11
|
+
* bare domain (`example.com/page`), `mailto:` before an email address, or undefined for anything
|
|
12
|
+
* else (another scheme, or text with spaces).
|
|
13
|
+
*
|
|
14
|
+
* @param text - What the user typed or pasted as the link.
|
|
15
|
+
*/
|
|
16
|
+
export declare function normalizeLinkUrl(text: string): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a key press is the link shortcut: ⌘⇧K, or Ctrl+Shift+K off the Mac. ⌘K stays Peers'
|
|
19
|
+
* command palette.
|
|
20
|
+
*
|
|
21
|
+
* @param event - The key press.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isLinkShortcut(event: KeyboardEvent): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Opens the link popup for editing: the link the caret is in, or a new link for the selected
|
|
26
|
+
* text (at the caret when nothing is selected). The toolbar's link button sends it.
|
|
27
|
+
*/
|
|
28
|
+
export declare const OPEN_LINK_EDITOR_COMMAND: LexicalCommand<void>;
|
|
29
|
+
/** The link a selection is in: the same link at both ends, or none. */
|
|
30
|
+
export declare function $selectedLink(selection: RangeSelection): LinkNode | null;
|
|
31
|
+
/**
|
|
32
|
+
* Links in the editor:
|
|
33
|
+
* - Clicking a web or mail link opens it in the browser (through the host's open). A click that
|
|
34
|
+
* selects text in a link doesn't.
|
|
35
|
+
* - ⌘⇧K (Ctrl+Shift+K) or the toolbar's link button links the selected text, or edits the link
|
|
36
|
+
* the caret is in.
|
|
37
|
+
* - Pasting a URL over selected text links it.
|
|
38
|
+
* - With the caret in a link, or its text selected, a popup shows the URL, Edit and Remove.
|
|
39
|
+
* Links found in the text (typed URLs) have no Remove, since the text makes them again.
|
|
40
|
+
*/
|
|
41
|
+
export declare function LinksPlugin(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OPEN_LINK_EDITOR_COMMAND = void 0;
|
|
4
|
+
exports.isLinkUrl = isLinkUrl;
|
|
5
|
+
exports.normalizeLinkUrl = normalizeLinkUrl;
|
|
6
|
+
exports.isLinkShortcut = isLinkShortcut;
|
|
7
|
+
exports.$selectedLink = $selectedLink;
|
|
8
|
+
exports.LinksPlugin = LinksPlugin;
|
|
9
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
10
|
+
const link_1 = require("@lexical/link");
|
|
11
|
+
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
|
|
12
|
+
const LexicalLinkPlugin_1 = require("@lexical/react/LexicalLinkPlugin");
|
|
13
|
+
const utils_1 = require("@lexical/utils");
|
|
14
|
+
const lexical_1 = require("lexical");
|
|
15
|
+
const react_1 = require("react");
|
|
16
|
+
const react_dom_1 = require("react-dom");
|
|
17
|
+
const host_shell_consumer_1 = require("../../host-shell/host-shell-consumer");
|
|
18
|
+
/** The link schemes the editor makes and opens: web links and email. */
|
|
19
|
+
const LINK_SCHEMES = new Set(["http:", "https:", "mailto:"]);
|
|
20
|
+
/**
|
|
21
|
+
* Whether `url` is a link the editor keeps: an `http:`, `https:` or `mailto:` URL.
|
|
22
|
+
*
|
|
23
|
+
* @param url - The text to check.
|
|
24
|
+
*/
|
|
25
|
+
function isLinkUrl(url) {
|
|
26
|
+
try {
|
|
27
|
+
return LINK_SCHEMES.has(new URL(url).protocol);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const EMAIL = /^[^\s@<>()[\]\\,;:"]+@[^\s@<>()[\]\\,;:"]+\.[a-z]{2,}$/i;
|
|
34
|
+
const BARE_DOMAIN = /^[\w-]+(\.[\w-]+)+(:\d+)?([/?#]\S*)?$/i;
|
|
35
|
+
/**
|
|
36
|
+
* What a typed link becomes: the URL as given when it's a web or mail link, `https://` before a
|
|
37
|
+
* bare domain (`example.com/page`), `mailto:` before an email address, or undefined for anything
|
|
38
|
+
* else (another scheme, or text with spaces).
|
|
39
|
+
*
|
|
40
|
+
* @param text - What the user typed or pasted as the link.
|
|
41
|
+
*/
|
|
42
|
+
function normalizeLinkUrl(text) {
|
|
43
|
+
const value = text.trim();
|
|
44
|
+
if (!value || /\s/.test(value))
|
|
45
|
+
return undefined;
|
|
46
|
+
if (isLinkUrl(value))
|
|
47
|
+
return value;
|
|
48
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(value) && !BARE_DOMAIN.test(value))
|
|
49
|
+
return undefined;
|
|
50
|
+
if (EMAIL.test(value))
|
|
51
|
+
return `mailto:${value}`;
|
|
52
|
+
if (BARE_DOMAIN.test(value))
|
|
53
|
+
return `https://${value}`;
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether a key press is the link shortcut: ⌘⇧K, or Ctrl+Shift+K off the Mac. ⌘K stays Peers'
|
|
58
|
+
* command palette.
|
|
59
|
+
*
|
|
60
|
+
* @param event - The key press.
|
|
61
|
+
*/
|
|
62
|
+
function isLinkShortcut(event) {
|
|
63
|
+
return ((event.metaKey || event.ctrlKey) &&
|
|
64
|
+
event.shiftKey &&
|
|
65
|
+
!event.altKey &&
|
|
66
|
+
event.key.toLowerCase() === "k");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Opens the link popup for editing: the link the caret is in, or a new link for the selected
|
|
70
|
+
* text (at the caret when nothing is selected). The toolbar's link button sends it.
|
|
71
|
+
*/
|
|
72
|
+
exports.OPEN_LINK_EDITOR_COMMAND = (0, lexical_1.createCommand)("OPEN_LINK_EDITOR_COMMAND");
|
|
73
|
+
/** The link a selection is in: the same link at both ends, or none. */
|
|
74
|
+
function $selectedLink(selection) {
|
|
75
|
+
const anchor = (0, utils_1.$findMatchingParent)(selection.anchor.getNode(), link_1.$isLinkNode);
|
|
76
|
+
const focus = (0, utils_1.$findMatchingParent)(selection.focus.getNode(), link_1.$isLinkNode);
|
|
77
|
+
return anchor && focus && anchor.is(focus) ? anchor : null;
|
|
78
|
+
}
|
|
79
|
+
/** The selection's rectangle on screen, or its anchor's element when the DOM has no range. */
|
|
80
|
+
function selectionRect(editor, selection) {
|
|
81
|
+
const root = editor.getRootElement();
|
|
82
|
+
const domSelection = window.getSelection();
|
|
83
|
+
if (root && domSelection && domSelection.rangeCount > 0) {
|
|
84
|
+
const range = domSelection.getRangeAt(0);
|
|
85
|
+
if (root.contains(range.startContainer)) {
|
|
86
|
+
const rect = range.getBoundingClientRect();
|
|
87
|
+
if (rect.width || rect.height)
|
|
88
|
+
return rect;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const node = selection.anchor.getNode();
|
|
92
|
+
const element = editor.getElementByKey(node.getKey()) ??
|
|
93
|
+
editor.getElementByKey(node.getTopLevelElementOrThrow().getKey());
|
|
94
|
+
return element?.getBoundingClientRect();
|
|
95
|
+
}
|
|
96
|
+
/** The popup for the link at `node`, positioned under its element. */
|
|
97
|
+
function popupFor(editor, link) {
|
|
98
|
+
const rect = editor.getElementByKey(link.getKey())?.getBoundingClientRect();
|
|
99
|
+
if (!rect)
|
|
100
|
+
return undefined;
|
|
101
|
+
return {
|
|
102
|
+
linkKey: link.getKey(),
|
|
103
|
+
url: link.getURL(),
|
|
104
|
+
auto: (0, link_1.$isAutoLinkNode)(link),
|
|
105
|
+
editing: false,
|
|
106
|
+
rect,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Links in the editor:
|
|
111
|
+
* - Clicking a web or mail link opens it in the browser (through the host's open). A click that
|
|
112
|
+
* selects text in a link doesn't.
|
|
113
|
+
* - ⌘⇧K (Ctrl+Shift+K) or the toolbar's link button links the selected text, or edits the link
|
|
114
|
+
* the caret is in.
|
|
115
|
+
* - Pasting a URL over selected text links it.
|
|
116
|
+
* - With the caret in a link, or its text selected, a popup shows the URL, Edit and Remove.
|
|
117
|
+
* Links found in the text (typed URLs) have no Remove, since the text makes them again.
|
|
118
|
+
*/
|
|
119
|
+
function LinksPlugin() {
|
|
120
|
+
const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
|
|
121
|
+
const [popup, setPopup] = (0, react_1.useState)();
|
|
122
|
+
const popupRef = (0, react_1.useRef)(null);
|
|
123
|
+
// The selection when the popup opened for editing, put back before the link is written.
|
|
124
|
+
const savedSelection = (0, react_1.useRef)(null);
|
|
125
|
+
// Show the popup for the link the selection is in, unless its URL is being edited.
|
|
126
|
+
const refresh = (0, react_1.useCallback)(() => {
|
|
127
|
+
setPopup((current) => {
|
|
128
|
+
if (current?.editing)
|
|
129
|
+
return current;
|
|
130
|
+
const root = editor.getRootElement();
|
|
131
|
+
const active = document.activeElement;
|
|
132
|
+
const focused = !!root && !!active && (root === active || root.contains(active));
|
|
133
|
+
const inPopup = !!active && !!popupRef.current?.contains(active);
|
|
134
|
+
if (!focused && !inPopup)
|
|
135
|
+
return undefined;
|
|
136
|
+
return editor.read(() => {
|
|
137
|
+
const selection = (0, lexical_1.$getSelection)();
|
|
138
|
+
if (!(0, lexical_1.$isRangeSelection)(selection))
|
|
139
|
+
return undefined;
|
|
140
|
+
const link = $selectedLink(selection);
|
|
141
|
+
return link ? popupFor(editor, link) : undefined;
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}, [editor]);
|
|
145
|
+
const openEditor = (0, react_1.useCallback)(() => {
|
|
146
|
+
editor.read(() => {
|
|
147
|
+
const selection = (0, lexical_1.$getSelection)();
|
|
148
|
+
if (!(0, lexical_1.$isRangeSelection)(selection))
|
|
149
|
+
return;
|
|
150
|
+
savedSelection.current = selection.clone();
|
|
151
|
+
const link = $selectedLink(selection);
|
|
152
|
+
const rect = link
|
|
153
|
+
? editor.getElementByKey(link.getKey())?.getBoundingClientRect()
|
|
154
|
+
: selectionRect(editor, selection);
|
|
155
|
+
if (!rect)
|
|
156
|
+
return;
|
|
157
|
+
setPopup({
|
|
158
|
+
linkKey: link?.getKey(),
|
|
159
|
+
url: link?.getURL() ?? "",
|
|
160
|
+
auto: link ? (0, link_1.$isAutoLinkNode)(link) : false,
|
|
161
|
+
editing: true,
|
|
162
|
+
rect,
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}, [editor]);
|
|
166
|
+
(0, react_1.useEffect)(() => {
|
|
167
|
+
const onClick = (event) => {
|
|
168
|
+
const target = event.target;
|
|
169
|
+
if (!(target instanceof Node) || event.button !== 0)
|
|
170
|
+
return;
|
|
171
|
+
// A click that ends a drag selecting text in the link doesn't open it.
|
|
172
|
+
if (window.getSelection()?.isCollapsed === false)
|
|
173
|
+
return;
|
|
174
|
+
let url;
|
|
175
|
+
editor.read(() => {
|
|
176
|
+
const node = (0, lexical_1.$getNearestNodeFromDOMNode)(target);
|
|
177
|
+
const link = node && (0, utils_1.$findMatchingParent)(node, link_1.$isLinkNode);
|
|
178
|
+
if (link)
|
|
179
|
+
url = link.getURL();
|
|
180
|
+
});
|
|
181
|
+
if (!url)
|
|
182
|
+
return;
|
|
183
|
+
event.preventDefault();
|
|
184
|
+
if (isLinkUrl(url))
|
|
185
|
+
void (0, host_shell_consumer_1.openLink)(url);
|
|
186
|
+
};
|
|
187
|
+
return (0, utils_1.mergeRegister)(editor.registerRootListener((root, previous) => {
|
|
188
|
+
previous?.removeEventListener("click", onClick);
|
|
189
|
+
root?.addEventListener("click", onClick);
|
|
190
|
+
}), editor.registerUpdateListener(() => refresh()), editor.registerCommand(lexical_1.BLUR_COMMAND, () => {
|
|
191
|
+
// Focus may be moving into the popup: look once it has landed.
|
|
192
|
+
setTimeout(refresh, 0);
|
|
193
|
+
return false;
|
|
194
|
+
}, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_DOWN_COMMAND, (event) => {
|
|
195
|
+
if (!isLinkShortcut(event))
|
|
196
|
+
return false;
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
event.stopPropagation();
|
|
199
|
+
openEditor();
|
|
200
|
+
return true;
|
|
201
|
+
}, lexical_1.COMMAND_PRIORITY_NORMAL), editor.registerCommand(exports.OPEN_LINK_EDITOR_COMMAND, () => {
|
|
202
|
+
openEditor();
|
|
203
|
+
return true;
|
|
204
|
+
}, lexical_1.COMMAND_PRIORITY_LOW));
|
|
205
|
+
}, [editor, refresh, openEditor]);
|
|
206
|
+
// Keep the popup under its link while the page or the editor scrolls.
|
|
207
|
+
const showing = !!popup;
|
|
208
|
+
(0, react_1.useEffect)(() => {
|
|
209
|
+
if (!showing)
|
|
210
|
+
return;
|
|
211
|
+
const follow = () => setPopup((current) => {
|
|
212
|
+
const element = current?.linkKey ? editor.getElementByKey(current.linkKey) : null;
|
|
213
|
+
return current && element ? { ...current, rect: element.getBoundingClientRect() } : current;
|
|
214
|
+
});
|
|
215
|
+
window.addEventListener("scroll", follow, true);
|
|
216
|
+
window.addEventListener("resize", follow);
|
|
217
|
+
return () => {
|
|
218
|
+
window.removeEventListener("scroll", follow, true);
|
|
219
|
+
window.removeEventListener("resize", follow);
|
|
220
|
+
};
|
|
221
|
+
}, [editor, showing]);
|
|
222
|
+
const close = (0, react_1.useCallback)((refocus) => {
|
|
223
|
+
setPopup(undefined);
|
|
224
|
+
savedSelection.current = null;
|
|
225
|
+
if (refocus)
|
|
226
|
+
editor.focus();
|
|
227
|
+
}, [editor]);
|
|
228
|
+
const save = (0, react_1.useCallback)((url) => {
|
|
229
|
+
const linkKey = popup?.linkKey;
|
|
230
|
+
editor.update(() => {
|
|
231
|
+
const existing = linkKey ? (0, lexical_1.$getNodeByKey)(linkKey) : null;
|
|
232
|
+
if ((0, link_1.$isLinkNode)(existing)) {
|
|
233
|
+
// A new node either way: an edited link found in the text is the user's link now.
|
|
234
|
+
const link = (0, link_1.$createLinkNode)(url);
|
|
235
|
+
link.append(...existing.getChildren());
|
|
236
|
+
existing.replace(link);
|
|
237
|
+
link.selectEnd();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const saved = savedSelection.current;
|
|
241
|
+
if (saved)
|
|
242
|
+
(0, lexical_1.$setSelection)(saved.clone());
|
|
243
|
+
const selection = (0, lexical_1.$getSelection)();
|
|
244
|
+
if (!(0, lexical_1.$isRangeSelection)(selection))
|
|
245
|
+
return;
|
|
246
|
+
if (selection.isCollapsed()) {
|
|
247
|
+
const link = (0, link_1.$createLinkNode)(url);
|
|
248
|
+
link.append((0, lexical_1.$createTextNode)(url));
|
|
249
|
+
selection.insertNodes([link]);
|
|
250
|
+
link.selectEnd();
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
(0, link_1.$toggleLink)(url);
|
|
254
|
+
}
|
|
255
|
+
}, { discrete: true });
|
|
256
|
+
close(true);
|
|
257
|
+
}, [editor, popup?.linkKey, close]);
|
|
258
|
+
const remove = (0, react_1.useCallback)(() => {
|
|
259
|
+
const linkKey = popup?.linkKey;
|
|
260
|
+
editor.update(() => {
|
|
261
|
+
const link = linkKey ? (0, lexical_1.$getNodeByKey)(linkKey) : null;
|
|
262
|
+
if (!(0, link_1.$isLinkNode)(link))
|
|
263
|
+
return;
|
|
264
|
+
const children = link.getChildren();
|
|
265
|
+
for (const child of children)
|
|
266
|
+
link.insertBefore(child);
|
|
267
|
+
link.remove();
|
|
268
|
+
children[children.length - 1]?.selectEnd();
|
|
269
|
+
}, { discrete: true });
|
|
270
|
+
close(true);
|
|
271
|
+
}, [editor, popup?.linkKey, close]);
|
|
272
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(LexicalLinkPlugin_1.LinkPlugin, { validateUrl: isLinkUrl }), popup ? ((0, jsx_runtime_1.jsx)(LinkPopup, { popup: popup, popupRef: popupRef, onEdit: () => {
|
|
273
|
+
editor.read(() => {
|
|
274
|
+
const selection = (0, lexical_1.$getSelection)();
|
|
275
|
+
savedSelection.current = (0, lexical_1.$isRangeSelection)(selection) ? selection.clone() : null;
|
|
276
|
+
});
|
|
277
|
+
setPopup({ ...popup, editing: true });
|
|
278
|
+
}, onSave: save, onRemove: remove, onCancel: () => close(true), onDismiss: () => close(false) })) : null] }));
|
|
279
|
+
}
|
|
280
|
+
/** The popup's width, so it can be kept inside the window. */
|
|
281
|
+
const POPUP_WIDTH = 320;
|
|
282
|
+
/** The link popup: the URL with Edit and Remove, or an input for the URL. */
|
|
283
|
+
function LinkPopup(props) {
|
|
284
|
+
const { popup, popupRef, onDismiss } = props;
|
|
285
|
+
const [text, setText] = (0, react_1.useState)(popup.url);
|
|
286
|
+
const [invalid, setInvalid] = (0, react_1.useState)(false);
|
|
287
|
+
const inputRef = (0, react_1.useRef)(null);
|
|
288
|
+
(0, react_1.useEffect)(() => {
|
|
289
|
+
setText(popup.url);
|
|
290
|
+
setInvalid(false);
|
|
291
|
+
}, [popup.url, popup.editing]);
|
|
292
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
293
|
+
if (popup.editing)
|
|
294
|
+
inputRef.current?.focus();
|
|
295
|
+
}, [popup.editing]);
|
|
296
|
+
// Editing stays open while focus is in the input; a press anywhere else closes it.
|
|
297
|
+
(0, react_1.useEffect)(() => {
|
|
298
|
+
if (!popup.editing)
|
|
299
|
+
return;
|
|
300
|
+
const onPointerDown = (event) => {
|
|
301
|
+
if (!popupRef.current?.contains(event.target))
|
|
302
|
+
onDismiss();
|
|
303
|
+
};
|
|
304
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
305
|
+
return () => document.removeEventListener("pointerdown", onPointerDown, true);
|
|
306
|
+
}, [popup.editing, popupRef, onDismiss]);
|
|
307
|
+
const submit = () => {
|
|
308
|
+
const url = normalizeLinkUrl(text);
|
|
309
|
+
if (url)
|
|
310
|
+
props.onSave(url);
|
|
311
|
+
else
|
|
312
|
+
setInvalid(true);
|
|
313
|
+
};
|
|
314
|
+
const left = Math.max(8, Math.min(popup.rect.left, window.innerWidth - POPUP_WIDTH - 8));
|
|
315
|
+
const style = { top: popup.rect.bottom + 4, left, maxWidth: POPUP_WIDTH };
|
|
316
|
+
// Buttons keep the editor's focus and selection.
|
|
317
|
+
const keepFocus = (event) => event.preventDefault();
|
|
318
|
+
return (0, react_dom_1.createPortal)((0, jsx_runtime_1.jsx)("div", { ref: popupRef, className: "peers-link-popup", style: style, "data-testid": "link-popup", children: popup.editing ? ((0, jsx_runtime_1.jsxs)("form", { className: "d-flex gap-1 align-items-center", onSubmit: (event) => {
|
|
319
|
+
event.preventDefault();
|
|
320
|
+
submit();
|
|
321
|
+
}, children: [(0, jsx_runtime_1.jsx)("input", { ref: inputRef, className: `form-control form-control-sm ${invalid ? "is-invalid" : ""}`, value: text, placeholder: "https://\u2026", "aria-label": "Link URL", onChange: (event) => {
|
|
322
|
+
setText(event.target.value);
|
|
323
|
+
setInvalid(false);
|
|
324
|
+
}, onKeyDown: (event) => {
|
|
325
|
+
if (event.key === "Escape") {
|
|
326
|
+
event.preventDefault();
|
|
327
|
+
props.onCancel();
|
|
328
|
+
}
|
|
329
|
+
} }), (0, jsx_runtime_1.jsx)("button", { type: "submit", className: "btn btn-sm btn-primary", title: "Save link", children: (0, jsx_runtime_1.jsx)("i", { className: "bi bi-check-lg" }) })] })) : ((0, jsx_runtime_1.jsxs)("div", { className: "d-flex gap-1 align-items-center", children: [(0, jsx_runtime_1.jsx)("a", { href: popup.url, className: "peers-link-popup-url text-truncate", title: popup.url, onMouseDown: keepFocus, onClick: (event) => {
|
|
330
|
+
event.preventDefault();
|
|
331
|
+
if (isLinkUrl(popup.url))
|
|
332
|
+
void (0, host_shell_consumer_1.openLink)(popup.url);
|
|
333
|
+
}, children: popup.url.replace(/^mailto:/, "") }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-link p-1", title: "Edit link", "aria-label": "Edit link", onMouseDown: keepFocus, onClick: props.onEdit, children: (0, jsx_runtime_1.jsx)("i", { className: "bi bi-pencil" }) }), popup.auto ? null : ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-link p-1", title: "Remove link", "aria-label": "Remove link", onMouseDown: keepFocus, onClick: props.onRemove, children: (0, jsx_runtime_1.jsx)("i", { className: "bi bi-x-lg" }) }))] })) }), document.body);
|
|
334
|
+
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { type Transformer } from "@lexical/markdown";
|
|
2
2
|
import { type Observable } from "@peers-app/peers-sdk";
|
|
3
3
|
/**
|
|
4
|
-
* Markdown for documents (task bodies, notes)
|
|
5
|
-
* which would otherwise take `peers://` links
|
|
6
|
-
* `- [ ]` would read as a plain list item
|
|
4
|
+
* Markdown for documents (task bodies, notes), in this order:
|
|
5
|
+
* - mentions, ahead of Lexical's link transformer, which would otherwise take `peers://` links;
|
|
6
|
+
* - checklists, ahead of Lexical's lists, or `- [ ]` would read as a plain list item;
|
|
7
|
+
* - dividers (`***`) and callouts (`> [!NOTE]`), the callouts ahead of Lexical's quotes;
|
|
8
|
+
* - then Lexical's own, headings and `==highlight==` included, with code fences written by
|
|
9
|
+
* {@link CODE_BLOCK_TRANSFORMER}.
|
|
7
10
|
*/
|
|
8
11
|
export declare const documentMarkdownTransformers: Transformer[];
|
|
9
12
|
/**
|
|
@@ -15,10 +18,23 @@ export declare const documentMarkdownTransformers: Transformer[];
|
|
|
15
18
|
*/
|
|
16
19
|
export declare function withoutHeadings(transformers: readonly Transformer[]): Transformer[];
|
|
17
20
|
/**
|
|
18
|
-
* Markdown for short inline text such as messages
|
|
19
|
-
*
|
|
21
|
+
* Markdown for short inline text such as messages: the document transformers without headings,
|
|
22
|
+
* dividers and callouts, so a line starting with `#`, a `---` line and `[!NOTE]` stay text.
|
|
20
23
|
*/
|
|
21
24
|
export declare const inlineMarkdownTransformers: Transformer[];
|
|
25
|
+
/**
|
|
26
|
+
* Load markdown into the editor in an update, as an outside change. Lexical's import puts the
|
|
27
|
+
* caret at the start whenever the editor has a selection, and an editor that isn't focused then
|
|
28
|
+
* takes the focus, from the editor being typed in (the same note open in two editors, such as a
|
|
29
|
+
* journal day and the panel beside it: on a phone the keyboard closes). So an editor without the
|
|
30
|
+
* focus is left with no selection, and a focused one keeps its caret in the same block, the same
|
|
31
|
+
* distance into its text.
|
|
32
|
+
*
|
|
33
|
+
* @param markdown - The markdown to load.
|
|
34
|
+
* @param transformers - How it's read.
|
|
35
|
+
* @param focused - Whether the editor has the focus.
|
|
36
|
+
*/
|
|
37
|
+
export declare function $importOutsideMarkdown(markdown: string, transformers: Transformer[], focused: boolean): void;
|
|
22
38
|
/** {@link MarkdownPlugin}'s props. */
|
|
23
39
|
export interface IMarkdownPluginProps {
|
|
24
40
|
/** The markdown the editor shows, written back as the user edits. */
|
|
@@ -28,6 +44,7 @@ export interface IMarkdownPluginProps {
|
|
|
28
44
|
}
|
|
29
45
|
/**
|
|
30
46
|
* Keeps the editor and a markdown observable in step: the observable's value is loaded into the
|
|
31
|
-
* editor (without moving the caret or taking focus), and edits
|
|
47
|
+
* editor (without moving the caret or taking focus: {@link $importOutsideMarkdown}), and edits
|
|
48
|
+
* are written back as markdown.
|
|
32
49
|
*/
|
|
33
50
|
export declare function MarkdownPlugin(props: IMarkdownPluginProps): null;
|
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.inlineMarkdownTransformers = exports.documentMarkdownTransformers = void 0;
|
|
4
4
|
exports.withoutHeadings = withoutHeadings;
|
|
5
|
+
exports.$importOutsideMarkdown = $importOutsideMarkdown;
|
|
5
6
|
exports.MarkdownPlugin = MarkdownPlugin;
|
|
6
7
|
const list_1 = require("@lexical/list");
|
|
7
8
|
const markdown_1 = require("@lexical/markdown");
|
|
8
9
|
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
|
|
9
10
|
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
11
|
+
const lexical_1 = require("lexical");
|
|
10
12
|
const react_1 = require("react");
|
|
13
|
+
const blocks_1 = require("./blocks");
|
|
14
|
+
const code_plugin_1 = require("./code-plugin");
|
|
11
15
|
const mention_node_1 = require("./mention-node");
|
|
12
16
|
// Amount of spaces that define indentation level
|
|
13
17
|
const LIST_INDENT_SIZE = 4;
|
|
@@ -135,15 +139,20 @@ const OLD_MENTION_TRANSFORMER = {
|
|
|
135
139
|
},
|
|
136
140
|
};
|
|
137
141
|
/**
|
|
138
|
-
* Markdown for documents (task bodies, notes)
|
|
139
|
-
* which would otherwise take `peers://` links
|
|
140
|
-
* `- [ ]` would read as a plain list item
|
|
142
|
+
* Markdown for documents (task bodies, notes), in this order:
|
|
143
|
+
* - mentions, ahead of Lexical's link transformer, which would otherwise take `peers://` links;
|
|
144
|
+
* - checklists, ahead of Lexical's lists, or `- [ ]` would read as a plain list item;
|
|
145
|
+
* - dividers (`***`) and callouts (`> [!NOTE]`), the callouts ahead of Lexical's quotes;
|
|
146
|
+
* - then Lexical's own, headings and `==highlight==` included, with code fences written by
|
|
147
|
+
* {@link CODE_BLOCK_TRANSFORMER}.
|
|
141
148
|
*/
|
|
142
149
|
exports.documentMarkdownTransformers = [
|
|
143
150
|
MENTION_TRANSFORMER,
|
|
144
151
|
OLD_MENTION_TRANSFORMER,
|
|
145
152
|
CHECK_LIST_TRANSFORMER,
|
|
146
|
-
|
|
153
|
+
blocks_1.HORIZONTAL_RULE_TRANSFORMER,
|
|
154
|
+
blocks_1.CALLOUT_TRANSFORMER,
|
|
155
|
+
...markdown_1.TRANSFORMERS.map((transformer) => transformer === markdown_1.CODE ? code_plugin_1.CODE_BLOCK_TRANSFORMER : transformer),
|
|
147
156
|
];
|
|
148
157
|
/**
|
|
149
158
|
* The transformers without the heading one. It's recognized by its node's type, `"heading"`, not
|
|
@@ -159,13 +168,87 @@ function withoutHeadings(transformers) {
|
|
|
159
168
|
});
|
|
160
169
|
}
|
|
161
170
|
/**
|
|
162
|
-
* Markdown for short inline text such as messages
|
|
163
|
-
*
|
|
171
|
+
* Markdown for short inline text such as messages: the document transformers without headings,
|
|
172
|
+
* dividers and callouts, so a line starting with `#`, a `---` line and `[!NOTE]` stay text.
|
|
164
173
|
*/
|
|
165
|
-
exports.inlineMarkdownTransformers = withoutHeadings(exports.documentMarkdownTransformers);
|
|
174
|
+
exports.inlineMarkdownTransformers = withoutHeadings(exports.documentMarkdownTransformers).filter((transformer) => transformer !== blocks_1.HORIZONTAL_RULE_TRANSFORMER && transformer !== blocks_1.CALLOUT_TRANSFORMER);
|
|
175
|
+
/** The text nodes in `node`, itself included when it's one. */
|
|
176
|
+
function textsIn(node) {
|
|
177
|
+
if ((0, lexical_1.$isTextNode)(node))
|
|
178
|
+
return [node];
|
|
179
|
+
return (0, lexical_1.$isElementNode)(node) ? node.getAllTextNodes() : [];
|
|
180
|
+
}
|
|
181
|
+
/** The caret's block (a child of the root) and its offset in the block's text, if it's in one. */
|
|
182
|
+
function $caretSpot() {
|
|
183
|
+
const selection = (0, lexical_1.$getSelection)();
|
|
184
|
+
if (!(0, lexical_1.$isRangeSelection)(selection))
|
|
185
|
+
return undefined;
|
|
186
|
+
const point = selection.focus;
|
|
187
|
+
const node = point.getNode();
|
|
188
|
+
const block = node.getTopLevelElement();
|
|
189
|
+
if (!block)
|
|
190
|
+
return undefined;
|
|
191
|
+
const size = (texts) => texts.reduce((total, text) => total + text.getTextContentSize(), 0);
|
|
192
|
+
const texts = block.getAllTextNodes();
|
|
193
|
+
if (point.type === "text") {
|
|
194
|
+
const at = texts.findIndex((text) => text.is(node));
|
|
195
|
+
return { block: block.getIndexWithinParent(), offset: size(texts.slice(0, at)) + point.offset };
|
|
196
|
+
}
|
|
197
|
+
// On an element (an empty paragraph, say): the text before it, and in its children before the
|
|
198
|
+
// point.
|
|
199
|
+
const within = new Set(textsIn(node).map((text) => text.getKey()));
|
|
200
|
+
const before = ((0, lexical_1.$isElementNode)(node) ? node.getChildren().slice(0, point.offset) : []).flatMap(textsIn);
|
|
201
|
+
const outside = texts.filter((text) => !within.has(text.getKey()) && text.isBefore(node));
|
|
202
|
+
return { block: block.getIndexWithinParent(), offset: size(outside) + size(before) };
|
|
203
|
+
}
|
|
204
|
+
/** Put the caret back at a {@link CaretSpot}, as near as the new document allows. */
|
|
205
|
+
function $restoreCaret(spot) {
|
|
206
|
+
const root = (0, lexical_1.$getRoot)();
|
|
207
|
+
const block = root.getChildAtIndex(Math.min(spot.block, root.getChildrenSize() - 1));
|
|
208
|
+
if (!(0, lexical_1.$isElementNode)(block)) {
|
|
209
|
+
root.selectEnd();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
let left = spot.offset;
|
|
213
|
+
const texts = block.getAllTextNodes();
|
|
214
|
+
for (const text of texts) {
|
|
215
|
+
const size = text.getTextContentSize();
|
|
216
|
+
if (left <= size) {
|
|
217
|
+
text.select(left, left);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
left -= size;
|
|
221
|
+
}
|
|
222
|
+
const last = texts.at(-1);
|
|
223
|
+
if (last)
|
|
224
|
+
last.select(last.getTextContentSize(), last.getTextContentSize());
|
|
225
|
+
else
|
|
226
|
+
block.selectStart();
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Load markdown into the editor in an update, as an outside change. Lexical's import puts the
|
|
230
|
+
* caret at the start whenever the editor has a selection, and an editor that isn't focused then
|
|
231
|
+
* takes the focus, from the editor being typed in (the same note open in two editors, such as a
|
|
232
|
+
* journal day and the panel beside it: on a phone the keyboard closes). So an editor without the
|
|
233
|
+
* focus is left with no selection, and a focused one keeps its caret in the same block, the same
|
|
234
|
+
* distance into its text.
|
|
235
|
+
*
|
|
236
|
+
* @param markdown - The markdown to load.
|
|
237
|
+
* @param transformers - How it's read.
|
|
238
|
+
* @param focused - Whether the editor has the focus.
|
|
239
|
+
*/
|
|
240
|
+
function $importOutsideMarkdown(markdown, transformers, focused) {
|
|
241
|
+
const spot = focused ? $caretSpot() : undefined;
|
|
242
|
+
(0, markdown_1.$convertFromMarkdownString)(markdown, transformers, undefined, true, false);
|
|
243
|
+
if (spot)
|
|
244
|
+
$restoreCaret(spot);
|
|
245
|
+
else if (!focused)
|
|
246
|
+
(0, lexical_1.$setSelection)(null);
|
|
247
|
+
}
|
|
166
248
|
/**
|
|
167
249
|
* Keeps the editor and a markdown observable in step: the observable's value is loaded into the
|
|
168
|
-
* editor (without moving the caret or taking focus), and edits
|
|
250
|
+
* editor (without moving the caret or taking focus: {@link $importOutsideMarkdown}), and edits
|
|
251
|
+
* are written back as markdown.
|
|
169
252
|
*/
|
|
170
253
|
function MarkdownPlugin(props) {
|
|
171
254
|
const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
|
|
@@ -179,8 +262,10 @@ function MarkdownPlugin(props) {
|
|
|
179
262
|
return;
|
|
180
263
|
}
|
|
181
264
|
markdown = newMarkdown;
|
|
265
|
+
const root = editor.getRootElement();
|
|
266
|
+
const focused = !!root && root.contains(document.activeElement);
|
|
182
267
|
editor.update(() => {
|
|
183
|
-
(
|
|
268
|
+
$importOutsideMarkdown(markdown, transformers, focused);
|
|
184
269
|
});
|
|
185
270
|
}
|
|
186
271
|
setMarkdown();
|
|
@@ -46,6 +46,31 @@ export declare function pickerOptions(query: string | null, found: Found, declin
|
|
|
46
46
|
};
|
|
47
47
|
/** The kinds that more than one package answers for among these results. */
|
|
48
48
|
export declare function sharedKinds(results: IMentionSearchResult[]): Set<string>;
|
|
49
|
+
/** Where the picker's menu sits for one open of it. */
|
|
50
|
+
export interface MenuPlacement {
|
|
51
|
+
/** Above the caret, which is in the lower half of the window; else below it. */
|
|
52
|
+
above: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The preview card's side: right of the list (""), else left of it, else under it (a window
|
|
55
|
+
* too narrow for either, such as a tablet held upright).
|
|
56
|
+
*/
|
|
57
|
+
preview: "" | "is-preview-left" | "is-preview-below";
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Where the menu goes for a caret at `caret`, in window coordinates. The picker decides it once
|
|
61
|
+
* per open, so the menu holds still while results arrive and change its height, and while the
|
|
62
|
+
* caret moves right as you type.
|
|
63
|
+
*
|
|
64
|
+
* @param caret - The caret's top and left edges in the window.
|
|
65
|
+
* @param view - The window's size.
|
|
66
|
+
*/
|
|
67
|
+
export declare function placeMenu(caret: {
|
|
68
|
+
top: number;
|
|
69
|
+
left: number;
|
|
70
|
+
}, view?: {
|
|
71
|
+
innerWidth: number;
|
|
72
|
+
innerHeight: number;
|
|
73
|
+
}): MenuPlacement;
|
|
49
74
|
/**
|
|
50
75
|
* The mention picker: `<` finds everything (people, then records from every group the user is
|
|
51
76
|
* in), `@` people only, and a trigger a mention source declares (`#`) that source's records,
|