docblocks-react-ui 0.15.1 → 0.17.0
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/CHANGELOG.md +39 -0
- package/README.md +38 -1
- package/dist/{Editor-xRjCnKad.mjs → Editor-CbHCJZ6R.mjs} +846 -69
- package/dist/index.d.mts +18 -3
- package/dist/index.mjs +86 -39
- package/dist/mermaid/index.mjs +1 -1
- package/dist/styles.css +10 -0
- package/package.json +2 -2
|
@@ -4,7 +4,7 @@ import * as React from "react";
|
|
|
4
4
|
import { Component, Fragment, createContext, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
5
5
|
import { AnimatePresence, Reorder, useDragControls } from "framer-motion";
|
|
6
6
|
import { I18nextProvider, initReactI18next, useTranslation } from "react-i18next";
|
|
7
|
-
import { ATTRIBUTE_TYPES, BLOCK_REGISTRY, CALLOUT_ICONS, DEFAULT_TABLE_COLUMN_WIDTH, DetachedBlockError, InvalidAttributeError, InvalidAttrsError, InvalidMarkError, InvalidOperationError, createSelection, isHexColor, isSafeEmbedSrc, isSafeLinkHref, presenceSelectionOf } from "docblocks-core";
|
|
7
|
+
import { ATTRIBUTE_TYPES, BLOCK_REGISTRY, CALLOUT_ICONS, DEFAULT_TABLE_COLUMN_WIDTH, DetachedBlockError, InvalidAttributeError, InvalidAttrsError, InvalidMarkError, InvalidOperationError, createSelection, isHexColor, isSafeEmbedSrc, isSafeLinkHref, linkHrefKindOf, presenceSelectionOf } from "docblocks-core";
|
|
8
8
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
9
9
|
import { createInstance } from "i18next";
|
|
10
10
|
import { createStore as createStore$1 } from "zustand/vanilla";
|
|
@@ -2583,6 +2583,7 @@ function renderMarked(run, resolveMentionLabel) {
|
|
|
2583
2583
|
if (linkHref !== void 0) node = /* @__PURE__ */ jsx("a", {
|
|
2584
2584
|
href: linkHref,
|
|
2585
2585
|
rel: "noopener noreferrer",
|
|
2586
|
+
"data-slot": "docblocks/link",
|
|
2586
2587
|
className: LINK_CLASS,
|
|
2587
2588
|
children: node
|
|
2588
2589
|
});
|
|
@@ -2723,7 +2724,8 @@ function renderRunsInto(runs, container, resolveMentionLabel) {
|
|
|
2723
2724
|
if (run.marks.link !== void 0 && isSafeLinkHref(run.marks.link.href)) node = wrap(node, "a", {
|
|
2724
2725
|
href: run.marks.link.href,
|
|
2725
2726
|
rel: "noopener noreferrer",
|
|
2726
|
-
class: LINK_CLASS
|
|
2727
|
+
class: LINK_CLASS,
|
|
2728
|
+
"data-slot": "docblocks/link"
|
|
2727
2729
|
});
|
|
2728
2730
|
if (run.marks.bold === true) node = wrap(node, "strong");
|
|
2729
2731
|
if (run.marks.italic === true) node = wrap(node, "em");
|
|
@@ -3927,7 +3929,8 @@ const INIT_OPTIONS = {
|
|
|
3927
3929
|
"code": "Monospace",
|
|
3928
3930
|
"italic": "Italic",
|
|
3929
3931
|
"underline": "Underline",
|
|
3930
|
-
"strike": "Strikethrough"
|
|
3932
|
+
"strike": "Strikethrough",
|
|
3933
|
+
"link": "Link"
|
|
3931
3934
|
},
|
|
3932
3935
|
"align": {
|
|
3933
3936
|
"left": "Align left",
|
|
@@ -4028,6 +4031,16 @@ const INIT_OPTIONS = {
|
|
|
4028
4031
|
"addAttribute": "Add attribute",
|
|
4029
4032
|
"addConfirm": "Add",
|
|
4030
4033
|
"keyReserved": "The key cannot be used (a reserved document key)."
|
|
4034
|
+
},
|
|
4035
|
+
link: {
|
|
4036
|
+
"aria": "Link editor",
|
|
4037
|
+
"placeholder": "Paste or type a link address",
|
|
4038
|
+
"invalid": "Invalid link address",
|
|
4039
|
+
"remove": "Remove link",
|
|
4040
|
+
"open": "Open link",
|
|
4041
|
+
"copy": "Copy link address",
|
|
4042
|
+
"edit": "Edit link",
|
|
4043
|
+
"menuAria": "Link actions"
|
|
4031
4044
|
}
|
|
4032
4045
|
},
|
|
4033
4046
|
frame: { page: {
|
|
@@ -4451,7 +4464,8 @@ const INIT_OPTIONS = {
|
|
|
4451
4464
|
"code": "Моноширинный",
|
|
4452
4465
|
"italic": "Курсив",
|
|
4453
4466
|
"underline": "Подчёркнутый",
|
|
4454
|
-
"strike": "Зачёркнутый"
|
|
4467
|
+
"strike": "Зачёркнутый",
|
|
4468
|
+
"link": "Ссылка"
|
|
4455
4469
|
},
|
|
4456
4470
|
"align": {
|
|
4457
4471
|
"left": "Выровнять по левому краю",
|
|
@@ -4552,6 +4566,16 @@ const INIT_OPTIONS = {
|
|
|
4552
4566
|
"addAttribute": "Добавить атрибут",
|
|
4553
4567
|
"addConfirm": "Добавить",
|
|
4554
4568
|
"keyReserved": "Ключ использовать нельзя (зарезервированный ключ документа)."
|
|
4569
|
+
},
|
|
4570
|
+
link: {
|
|
4571
|
+
"aria": "Редактор ссылки",
|
|
4572
|
+
"placeholder": "Вставьте или введите адрес ссылки",
|
|
4573
|
+
"invalid": "Неверный адрес ссылки",
|
|
4574
|
+
"remove": "Убрать ссылку",
|
|
4575
|
+
"open": "Открыть ссылку",
|
|
4576
|
+
"copy": "Копировать адрес ссылки",
|
|
4577
|
+
"edit": "Изменить ссылку",
|
|
4578
|
+
"menuAria": "Действия со ссылкой"
|
|
4555
4579
|
}
|
|
4556
4580
|
},
|
|
4557
4581
|
frame: { page: {
|
|
@@ -17180,6 +17204,92 @@ function isFreshForMeta(doc, seatId) {
|
|
|
17180
17204
|
return false;
|
|
17181
17205
|
}
|
|
17182
17206
|
//#endregion
|
|
17207
|
+
//#region src/editing/linkify.ts
|
|
17208
|
+
/**
|
|
17209
|
+
* The plain-text paste's linkify (the paste's convenience's stand): the
|
|
17210
|
+
* bare web's URLs and the email's forms stand the link's mark over the
|
|
17211
|
+
* plain's runs — a match splits the run into the plain's parts and the
|
|
17212
|
+
* linked span (the span keeps the run's own marks). The code's block / the
|
|
17213
|
+
* exclusive code's mark / the already-linked's / the mention's runs stand
|
|
17214
|
+
* untouched. The phone's forms stand no auto-link (the digits' false's
|
|
17215
|
+
* stand — the versions' / the dates' / the numbers' tokens); the phone's
|
|
17216
|
+
* link is the manual's stand (the link editor's `tel:`'s inference).
|
|
17217
|
+
*
|
|
17218
|
+
* The markdown's explicit links (`[t](u)` / the autolink's `<https://…>`)
|
|
17219
|
+
* stand the parser's runs (the link's mark already) and pass through.
|
|
17220
|
+
* The faithful import's path (the headless's `importDocument`) stands no
|
|
17221
|
+
* linkify — the markdown's semantics stand the explicit's links only.
|
|
17222
|
+
*/
|
|
17223
|
+
/** The combined scan: the bare web's URL or the email's form, the
|
|
17224
|
+
* leftmost's match wins. The URL's look-behind keeps the token's middle
|
|
17225
|
+
* (`xhttps://…` stands the plain); the email's look-behind keeps the
|
|
17226
|
+
* local's token's boundary (the `.` / the `+` / the `-`'s continuation's
|
|
17227
|
+
* stand, the `:`'s exclusion — the `mailto:`'s prefix's residue stands
|
|
17228
|
+
* the plain, not the re-linked's stand). */
|
|
17229
|
+
const LINKIFY_RE = /(?<![A-Za-z0-9])(https?:\/\/[^\s<>"']+)|(?<![\w.:%+-])([A-Za-z0-9._%+-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}(?![A-Za-z0-9-]))/g;
|
|
17230
|
+
/** The bare URL's trailing punctuation (the sentence's end's stand). */
|
|
17231
|
+
const URL_TRAILING_PUNCT = /[.,;:!?]+$/;
|
|
17232
|
+
/** The bare URL's href (the trailing punctuation's trim + the unbalanced
|
|
17233
|
+
* closing paren's drop + the non-empty body's gate over the core's
|
|
17234
|
+
* contract; `null` stands the plain's degradation). */
|
|
17235
|
+
function bareUrlHref(url) {
|
|
17236
|
+
let body = url.replace(URL_TRAILING_PUNCT, "");
|
|
17237
|
+
if (body.endsWith(")") && (body.match(/\(/g)?.length ?? 0) < (body.match(/\)/g)?.length ?? 0)) body = body.slice(0, -1);
|
|
17238
|
+
const schemeEnd = body.indexOf("//") + 2;
|
|
17239
|
+
if (schemeEnd < 2 || body.length <= schemeEnd) return null;
|
|
17240
|
+
return isSafeLinkHref(body) ? body : null;
|
|
17241
|
+
}
|
|
17242
|
+
/** One plain run's text over the link's spans: the plain's parts (no
|
|
17243
|
+
* link) and the linked's spans (the href — the URL's trimmed form, the
|
|
17244
|
+
* email's `mailto:`'s form). */
|
|
17245
|
+
function linkifyText(text) {
|
|
17246
|
+
const parts = [];
|
|
17247
|
+
let last = 0;
|
|
17248
|
+
for (const match of text.matchAll(LINKIFY_RE)) {
|
|
17249
|
+
const start = match.index ?? 0;
|
|
17250
|
+
const url = match[1];
|
|
17251
|
+
let href = null;
|
|
17252
|
+
let span = match[0].length;
|
|
17253
|
+
if (url !== void 0) {
|
|
17254
|
+
href = bareUrlHref(url);
|
|
17255
|
+
if (href !== null) span = href.length;
|
|
17256
|
+
} else {
|
|
17257
|
+
const candidate = `mailto:${match[2]}`;
|
|
17258
|
+
if (isSafeLinkHref(candidate)) href = candidate;
|
|
17259
|
+
}
|
|
17260
|
+
if (start > last) parts.push({ text: text.slice(last, start) });
|
|
17261
|
+
if (href !== null) parts.push({
|
|
17262
|
+
text: text.slice(start, start + span),
|
|
17263
|
+
link: href
|
|
17264
|
+
});
|
|
17265
|
+
else parts.push({ text: text.slice(start, start + span) });
|
|
17266
|
+
last = start + span;
|
|
17267
|
+
}
|
|
17268
|
+
if (last < text.length) parts.push({ text: text.slice(last) });
|
|
17269
|
+
return parts;
|
|
17270
|
+
}
|
|
17271
|
+
/** The runs' linkify: the plain's text's runs stand the split over the
|
|
17272
|
+
* matches (the span keeps the run's marks); the code's / the linked's /
|
|
17273
|
+
* the mention's runs pass through. */
|
|
17274
|
+
function linkifyRuns(runs) {
|
|
17275
|
+
const out = [];
|
|
17276
|
+
for (const run of runs) {
|
|
17277
|
+
const { marks, text } = run;
|
|
17278
|
+
if (text === "" || marks.code === true || marks.link !== void 0 || marks.mention !== void 0) {
|
|
17279
|
+
out.push(run);
|
|
17280
|
+
continue;
|
|
17281
|
+
}
|
|
17282
|
+
for (const part of linkifyText(text)) out.push({
|
|
17283
|
+
text: part.text,
|
|
17284
|
+
marks: part.link !== void 0 ? {
|
|
17285
|
+
...marks,
|
|
17286
|
+
link: { href: part.link }
|
|
17287
|
+
} : marks
|
|
17288
|
+
});
|
|
17289
|
+
}
|
|
17290
|
+
return out;
|
|
17291
|
+
}
|
|
17292
|
+
//#endregion
|
|
17183
17293
|
//#region src/editing/mark-utils.ts
|
|
17184
17294
|
/** The shared mark-set equality (the run merge): the same keys with the same values. */
|
|
17185
17295
|
function marksEqual(a, b) {
|
|
@@ -18109,6 +18219,17 @@ function insertTable(doc, m, parent, after, indent) {
|
|
|
18109
18219
|
}
|
|
18110
18220
|
//#endregion
|
|
18111
18221
|
//#region src/editing/clipboard/utils/markdown.ts
|
|
18222
|
+
/** The paste's linkify's pass (the `text/plain`'s branch's stand — the
|
|
18223
|
+
* markdown's faithful import's path stands the explicit's links only):
|
|
18224
|
+
* the bare's URLs + the email's forms over the plain's runs; the code's
|
|
18225
|
+
* block's runs stand untouched (the fence's syntax is the code's data). */
|
|
18226
|
+
function linkifyPasteBlocks(blocks) {
|
|
18227
|
+
return blocks.map((block) => block.type === "code" ? block : {
|
|
18228
|
+
...block,
|
|
18229
|
+
runs: linkifyRuns(block.runs),
|
|
18230
|
+
rows: block.rows?.map((row) => row.map((cell) => linkifyRuns(cell)))
|
|
18231
|
+
});
|
|
18232
|
+
}
|
|
18112
18233
|
function applyMdPaste(doc, seatId, from, to, markdown, opts) {
|
|
18113
18234
|
if (doc.getBlock(seatId)?.type === "code") {
|
|
18114
18235
|
const raw = markdown.replace(/\r\n?/g, "\n");
|
|
@@ -18128,11 +18249,11 @@ function applyMdPaste(doc, seatId, from, to, markdown, opts) {
|
|
|
18128
18249
|
doc.transact(() => {
|
|
18129
18250
|
if (meta.title !== void 0) doc.setTitle(meta.title);
|
|
18130
18251
|
for (const [key, value] of Object.entries(meta.attributes)) doc.setAttribute(key, value);
|
|
18131
|
-
landing = materializePaste(doc, seatId, from, to, mdToBlocks(frontmatter.body), opts);
|
|
18252
|
+
landing = materializePaste(doc, seatId, from, to, linkifyPasteBlocks(mdToBlocks(frontmatter.body)), opts);
|
|
18132
18253
|
});
|
|
18133
18254
|
return landing;
|
|
18134
18255
|
}
|
|
18135
|
-
return materializePaste(doc, seatId, from, to, mdToBlocks(markdown), opts);
|
|
18256
|
+
return materializePaste(doc, seatId, from, to, linkifyPasteBlocks(mdToBlocks(markdown)), opts);
|
|
18136
18257
|
}
|
|
18137
18258
|
//#endregion
|
|
18138
18259
|
//#region src/editing/clipboard/utils/html.ts
|
|
@@ -22595,6 +22716,26 @@ function crossRangeOf(selection) {
|
|
|
22595
22716
|
chain: selection.blocks ?? [selection.from.blockId, selection.to.blockId]
|
|
22596
22717
|
};
|
|
22597
22718
|
}
|
|
22719
|
+
/** The format target range over a text view selection (a same-block range
|
|
22720
|
+
* or a cross-block range over the engine's chain; `null` over a caret, a
|
|
22721
|
+
* non-text view, or an ineligible cross chain). */
|
|
22722
|
+
function toolbarRangeOf(doc, selection) {
|
|
22723
|
+
if (selection.kind !== "text" || selection.to === null) return null;
|
|
22724
|
+
if (selection.from.blockId === selection.to.blockId) {
|
|
22725
|
+
if (selection.from.offset === selection.to.offset) return null;
|
|
22726
|
+
return {
|
|
22727
|
+
kind: "single",
|
|
22728
|
+
blockId: selection.from.blockId,
|
|
22729
|
+
from: selection.from.offset,
|
|
22730
|
+
to: selection.to.offset
|
|
22731
|
+
};
|
|
22732
|
+
}
|
|
22733
|
+
const cross = crossRangeOf(selection);
|
|
22734
|
+
return cross !== null && crossRangeEligible(doc, cross) ? {
|
|
22735
|
+
kind: "cross",
|
|
22736
|
+
cross
|
|
22737
|
+
} : null;
|
|
22738
|
+
}
|
|
22598
22739
|
/** Whether every text block of the chain is a toolbar-eligible type. */
|
|
22599
22740
|
function crossRangeEligible(doc, range) {
|
|
22600
22741
|
for (const id of range.chain) {
|
|
@@ -22736,6 +22877,659 @@ function uniformMarkValueRange(doc, range, mark) {
|
|
|
22736
22877
|
return seen ? value ?? "" : null;
|
|
22737
22878
|
}
|
|
22738
22879
|
//#endregion
|
|
22880
|
+
//#region src/editing/link/link-popover-context.ts
|
|
22881
|
+
/** The link popover's api's context (the editor-level's opener over the
|
|
22882
|
+
* selection and the explicit anchor — the format toolbar's button reads
|
|
22883
|
+
* it; the provider stands in the editor's root subtree). */
|
|
22884
|
+
const LinkPopoverContext = createContext(null);
|
|
22885
|
+
/** The link popover's api over the provider (`null` over the provider's
|
|
22886
|
+
* absence — the link's affordances' stand is off). */
|
|
22887
|
+
function useLinkPopoverApi() {
|
|
22888
|
+
return useContext(LinkPopoverContext);
|
|
22889
|
+
}
|
|
22890
|
+
//#endregion
|
|
22891
|
+
//#region src/editing/link/LinkMenu.tsx
|
|
22892
|
+
/** The link's right-click menu (the anchor's actions): the open + the
|
|
22893
|
+
* copy's address stand over the edit and the view's modes, the edit + the
|
|
22894
|
+
* remove stand over the edit's mode only (the read-only's stand). A plain
|
|
22895
|
+
* fixed-positioned div (the viewport's coords) — no Radix menu (the
|
|
22896
|
+
* release-open's buttonless-up select's stand is the Radix item's gap, not
|
|
22897
|
+
* the plain button's). */
|
|
22898
|
+
/** The menu's width (px — the clamp's measure). */
|
|
22899
|
+
const MENU_WIDTH_PX = 224;
|
|
22900
|
+
/** The menu's viewport's edge margin (px). */
|
|
22901
|
+
const VIEWPORT_MARGIN_PX = 6;
|
|
22902
|
+
function LinkMenu({ menu, viewMode, onOpen, onCopy, onEdit, onRemove, onClose }) {
|
|
22903
|
+
const { t } = useTranslation("editor");
|
|
22904
|
+
const menuRef = useRef(null);
|
|
22905
|
+
const [position, setPosition] = useState(null);
|
|
22906
|
+
useLayoutEffect(() => {
|
|
22907
|
+
const el = menuRef.current;
|
|
22908
|
+
if (el === null) return;
|
|
22909
|
+
const left = Math.max(VIEWPORT_MARGIN_PX, Math.min(menu.x, window.innerWidth - MENU_WIDTH_PX - VIEWPORT_MARGIN_PX));
|
|
22910
|
+
const top = Math.max(VIEWPORT_MARGIN_PX, Math.min(menu.y, window.innerHeight - el.offsetHeight - VIEWPORT_MARGIN_PX));
|
|
22911
|
+
setPosition({
|
|
22912
|
+
left,
|
|
22913
|
+
top
|
|
22914
|
+
});
|
|
22915
|
+
}, [menu.x, menu.y]);
|
|
22916
|
+
const handleOpen = () => onOpen();
|
|
22917
|
+
const handleCopy = () => onCopy();
|
|
22918
|
+
const handleEdit = () => onEdit();
|
|
22919
|
+
const handleRemove = () => onRemove();
|
|
22920
|
+
useLayoutEffect(() => {
|
|
22921
|
+
const onKey = (event) => {
|
|
22922
|
+
if (event.key === "Escape") onClose();
|
|
22923
|
+
};
|
|
22924
|
+
const onPointerDown = (event) => {
|
|
22925
|
+
const el = menuRef.current;
|
|
22926
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
22927
|
+
if (el !== null && target !== null && !el.contains(target)) onClose();
|
|
22928
|
+
};
|
|
22929
|
+
document.addEventListener("keydown", onKey, true);
|
|
22930
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
22931
|
+
return () => {
|
|
22932
|
+
document.removeEventListener("keydown", onKey, true);
|
|
22933
|
+
document.removeEventListener("pointerdown", onPointerDown, true);
|
|
22934
|
+
};
|
|
22935
|
+
}, [onClose]);
|
|
22936
|
+
const item = (label, action) => /* @__PURE__ */ jsx(Button, {
|
|
22937
|
+
role: "menuitem",
|
|
22938
|
+
variant: "ghost",
|
|
22939
|
+
size: "sm",
|
|
22940
|
+
className: "h-8 w-full justify-start px-2 text-sm",
|
|
22941
|
+
onClick: action,
|
|
22942
|
+
children: label
|
|
22943
|
+
});
|
|
22944
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
22945
|
+
ref: menuRef,
|
|
22946
|
+
role: "menu",
|
|
22947
|
+
"data-slot": "docblocks/link-menu",
|
|
22948
|
+
"aria-label": t("link.menuAria"),
|
|
22949
|
+
className: cn("fixed z-modal flex flex-col gap-0.5 rounded-md border border-border bg-popover p-1 text-popover-foreground", "shadow-md", position === null && "opacity-0"),
|
|
22950
|
+
style: position === null ? void 0 : {
|
|
22951
|
+
left: position.left,
|
|
22952
|
+
top: position.top,
|
|
22953
|
+
width: MENU_WIDTH_PX
|
|
22954
|
+
},
|
|
22955
|
+
onMouseDown: preventDefault,
|
|
22956
|
+
onContextMenu: (event) => event.preventDefault(),
|
|
22957
|
+
children: [
|
|
22958
|
+
item(t("link.open"), handleOpen),
|
|
22959
|
+
item(t("link.copy"), handleCopy),
|
|
22960
|
+
!viewMode && item(t("link.edit"), handleEdit),
|
|
22961
|
+
!viewMode && item(t("link.remove"), handleRemove)
|
|
22962
|
+
]
|
|
22963
|
+
});
|
|
22964
|
+
}
|
|
22965
|
+
//#endregion
|
|
22966
|
+
//#region src/editing/link/range.ts
|
|
22967
|
+
function spansOf(doc, blockId) {
|
|
22968
|
+
const spans = [];
|
|
22969
|
+
let pos = 0;
|
|
22970
|
+
for (const run of doc.getRuns(blockId)) {
|
|
22971
|
+
spans.push({
|
|
22972
|
+
start: pos,
|
|
22973
|
+
end: pos + run.text.length,
|
|
22974
|
+
href: run.marks.link?.href ?? null
|
|
22975
|
+
});
|
|
22976
|
+
pos += run.text.length;
|
|
22977
|
+
}
|
|
22978
|
+
return spans;
|
|
22979
|
+
}
|
|
22980
|
+
/** The range's uniform link href (single / cross): the href when every
|
|
22981
|
+
* segment's every overlapping run carries the link with it; `null` over
|
|
22982
|
+
* the absent / the mixed's stand. */
|
|
22983
|
+
function uniformLinkHrefOf(doc, target) {
|
|
22984
|
+
const segments = target.kind === "single" ? [[
|
|
22985
|
+
target.blockId,
|
|
22986
|
+
target.from,
|
|
22987
|
+
target.to
|
|
22988
|
+
]] : rangeSegments(doc, target.cross);
|
|
22989
|
+
let href = null;
|
|
22990
|
+
for (const [blockId, from, to] of segments) {
|
|
22991
|
+
if (from >= to) continue;
|
|
22992
|
+
for (const span of spansOf(doc, blockId)) {
|
|
22993
|
+
if (Math.max(span.start, from) >= Math.min(span.end, to)) continue;
|
|
22994
|
+
if (span.href === null) return null;
|
|
22995
|
+
if (href === null) href = span.href;
|
|
22996
|
+
else if (span.href !== href) return null;
|
|
22997
|
+
}
|
|
22998
|
+
}
|
|
22999
|
+
return href;
|
|
23000
|
+
}
|
|
23001
|
+
/** The maximal uniform-link's span over a caret: the same href's
|
|
23002
|
+
* contiguous's runs containing the caret (a boundary caret belongs to the
|
|
23003
|
+
* preceding's run); `null` when the caret stands no link. */
|
|
23004
|
+
function linkRangeOf(doc, blockId, at) {
|
|
23005
|
+
const spans = spansOf(doc, blockId);
|
|
23006
|
+
let index = -1;
|
|
23007
|
+
for (let i = 0; i < spans.length; i += 1) {
|
|
23008
|
+
const span = spans[i];
|
|
23009
|
+
if (at > span.start && at <= span.end) {
|
|
23010
|
+
index = i;
|
|
23011
|
+
break;
|
|
23012
|
+
}
|
|
23013
|
+
if (at === span.start && i > 0) {
|
|
23014
|
+
index = i - 1;
|
|
23015
|
+
break;
|
|
23016
|
+
}
|
|
23017
|
+
}
|
|
23018
|
+
if (index === -1 || spans[index].href === null) return null;
|
|
23019
|
+
const href = spans[index].href;
|
|
23020
|
+
let from = spans[index].start;
|
|
23021
|
+
let to = spans[index].end;
|
|
23022
|
+
for (let i = index - 1; i >= 0 && spans[i].href === href; i -= 1) from = spans[i].start;
|
|
23023
|
+
for (let i = index + 1; i < spans.length && spans[i].href === href; i += 1) to = spans[i].end;
|
|
23024
|
+
return {
|
|
23025
|
+
from,
|
|
23026
|
+
to,
|
|
23027
|
+
href
|
|
23028
|
+
};
|
|
23029
|
+
}
|
|
23030
|
+
/** The caret's target (the mod+k's / the caret's gesture's stand): the
|
|
23031
|
+
* link's expansion over a caret inside a link, the caret's own insertion's
|
|
23032
|
+
* target otherwise (the no-selection's stand — the URL's text lands as the
|
|
23033
|
+
* link's label). */
|
|
23034
|
+
function linkTargetOfCaret(doc, point) {
|
|
23035
|
+
const expansion = linkRangeOf(doc, point.blockId, point.offset);
|
|
23036
|
+
if (expansion !== null) return {
|
|
23037
|
+
kind: "single",
|
|
23038
|
+
blockId: point.blockId,
|
|
23039
|
+
from: expansion.from,
|
|
23040
|
+
to: expansion.to
|
|
23041
|
+
};
|
|
23042
|
+
return {
|
|
23043
|
+
kind: "single",
|
|
23044
|
+
blockId: point.blockId,
|
|
23045
|
+
from: point.offset,
|
|
23046
|
+
to: point.offset
|
|
23047
|
+
};
|
|
23048
|
+
}
|
|
23049
|
+
//#endregion
|
|
23050
|
+
//#region src/editing/link/actions.ts
|
|
23051
|
+
/** Apply the link's href over the target (one undo step). Over a caret
|
|
23052
|
+
* (the no-selection's stand) the `label`'s text (the input's stand, the
|
|
23053
|
+
* href's stand over its absence) is inserted as the linked's run. */
|
|
23054
|
+
function applyLinkHref(doc, target, href, label) {
|
|
23055
|
+
if (uniformLinkHrefOf(doc, target) === href) return;
|
|
23056
|
+
if (target.kind === "cross") {
|
|
23057
|
+
doc.setMarkRange(target.cross.from, target.cross.to, {
|
|
23058
|
+
type: "link",
|
|
23059
|
+
href
|
|
23060
|
+
});
|
|
23061
|
+
return;
|
|
23062
|
+
}
|
|
23063
|
+
if (target.from === target.to) {
|
|
23064
|
+
const text = label ?? href;
|
|
23065
|
+
doc.transact(() => {
|
|
23066
|
+
doc.insertText(target.blockId, target.from, text);
|
|
23067
|
+
doc.setMark(target.blockId, target.from, target.from + text.length, {
|
|
23068
|
+
type: "link",
|
|
23069
|
+
href
|
|
23070
|
+
});
|
|
23071
|
+
});
|
|
23072
|
+
return;
|
|
23073
|
+
}
|
|
23074
|
+
doc.transact(() => {
|
|
23075
|
+
doc.setMark(target.blockId, target.from, target.to, {
|
|
23076
|
+
type: "link",
|
|
23077
|
+
href
|
|
23078
|
+
});
|
|
23079
|
+
});
|
|
23080
|
+
}
|
|
23081
|
+
/** Remove the link over the target (one undo step). */
|
|
23082
|
+
function removeLink(doc, target) {
|
|
23083
|
+
if (target.kind === "cross") {
|
|
23084
|
+
clearMarkRange(doc, target.cross, "link");
|
|
23085
|
+
return;
|
|
23086
|
+
}
|
|
23087
|
+
doc.transact(() => {
|
|
23088
|
+
doc.unsetMark(target.blockId, target.from, target.to, "link");
|
|
23089
|
+
});
|
|
23090
|
+
}
|
|
23091
|
+
//#endregion
|
|
23092
|
+
//#region src/editing/link/resolve.ts
|
|
23093
|
+
/** The link popover's input's resolution (the smart scheme's inference):
|
|
23094
|
+
* the explicit scheme (the `mailto:` / the `tel:` / the web's URL's form)
|
|
23095
|
+
* passes over the core's gate as-is; the protocol-relative form (the
|
|
23096
|
+
* leading slash / backslash pair) stands the explicit `https://`'s stand
|
|
23097
|
+
* (the stored href is self-describing — the reader's scheme never decides
|
|
23098
|
+
* it); the leading `/` / `#` / `.` stands the relative's form; the email's form (the `@` without the whitespace)
|
|
23099
|
+
* stands the `mailto:`; the phone's form (the digits + the separators, no
|
|
23100
|
+
* letters) stands the `tel:`; the bare's form (a domain / a path's text)
|
|
23101
|
+
* stands the `https://`'s prefix. `null` stands the rejection (the
|
|
23102
|
+
* popover's invalid's stand). */
|
|
23103
|
+
/** The explicit scheme's prefix (the `mailto:` / the `tel:` / the web's
|
|
23104
|
+
* URL's discriminator — the scheme's case-insensitive stand). */
|
|
23105
|
+
const SCHEME_PREFIX = /^[a-z][a-z0-9+.-]*:/i;
|
|
23106
|
+
/** The protocol-relative form (the browser's special-scheme's slash /
|
|
23107
|
+
* backslash equivalence — an absolute URL over the reader's scheme). */
|
|
23108
|
+
const PROTOCOL_RELATIVE = /^[/\\]{2}/;
|
|
23109
|
+
/** The phone's form: 5–20 characters of digits and the phone's separators
|
|
23110
|
+
* (no letters) — the versions' / the dates' tokens stand the https's
|
|
23111
|
+
* form over it (the digits' minimum is checked over the call). */
|
|
23112
|
+
const PHONE_FORM = /^[0-9()+\-'., ]{5,20}$/;
|
|
23113
|
+
/** The minimum digit count of the phone's form (the `tel:`'s payload's
|
|
23114
|
+
* parity over the core's contract). */
|
|
23115
|
+
const PHONE_MIN_DIGITS = 5;
|
|
23116
|
+
function resolveLinkHref(input) {
|
|
23117
|
+
const value = input.trim();
|
|
23118
|
+
if (value === "") return null;
|
|
23119
|
+
if (SCHEME_PREFIX.test(value)) return isSafeLinkHref(value) ? value : null;
|
|
23120
|
+
if (PROTOCOL_RELATIVE.test(value)) {
|
|
23121
|
+
const web = `https://${value.slice(2)}`;
|
|
23122
|
+
return isSafeLinkHref(web) ? web : null;
|
|
23123
|
+
}
|
|
23124
|
+
if (value.startsWith("/") || value.startsWith("#") || value.startsWith(".")) return isSafeLinkHref(value) ? value : null;
|
|
23125
|
+
if (value.includes("@") && !/\s/.test(value)) {
|
|
23126
|
+
const href = `mailto:${value}`;
|
|
23127
|
+
return isSafeLinkHref(href) ? href : null;
|
|
23128
|
+
}
|
|
23129
|
+
if (PHONE_FORM.test(value) && (value.match(/\d/g) ?? []).length >= PHONE_MIN_DIGITS) {
|
|
23130
|
+
const href = `tel:${value}`;
|
|
23131
|
+
return isSafeLinkHref(href) ? href : null;
|
|
23132
|
+
}
|
|
23133
|
+
if (/\s/.test(value)) return null;
|
|
23134
|
+
const web = `https://${value}`;
|
|
23135
|
+
return isSafeLinkHref(web) ? web : null;
|
|
23136
|
+
}
|
|
23137
|
+
//#endregion
|
|
23138
|
+
//#region src/editing/link/LinkPopover.tsx
|
|
23139
|
+
/** The link editor's floating popover: the one field (the address's input
|
|
23140
|
+
* over the smart scheme's inference — the `resolve`'s unit) + the
|
|
23141
|
+
* remove's button (over the linked's target's stand). A plain
|
|
23142
|
+
* absolutely-positioned div inside the editor root (the root is the
|
|
23143
|
+
* positioning context), like the slash / the mention's menus. Enter / the
|
|
23144
|
+
* apply stands the commit (the `applyLinkHref`'s one transaction), the
|
|
23145
|
+
* Escape stands the close (the no-write's stand), the invalid's stand
|
|
23146
|
+
* stands the destructive's border + the message. The popover's mousedown
|
|
23147
|
+
* stands the default's suppress (the selection's life over the edit).
|
|
23148
|
+
*/
|
|
23149
|
+
/** The popover's width (px — the input's field's stand). */
|
|
23150
|
+
const POPOVER_WIDTH_PX = 288;
|
|
23151
|
+
/** The anchor's offset from the popover's edge (px — the flip's margin). */
|
|
23152
|
+
const ANCHOR_OFFSET_PX = 8;
|
|
23153
|
+
function LinkPopover({ rootRef, state, onClose }) {
|
|
23154
|
+
const { t } = useTranslation("editor");
|
|
23155
|
+
const { doc } = useEditorContext();
|
|
23156
|
+
const popoverRef = useRef(null);
|
|
23157
|
+
const inputRef = useRef(null);
|
|
23158
|
+
const [value, setValue] = useState(state.prefilledHref ?? "");
|
|
23159
|
+
const [invalid, setInvalid] = useState(false);
|
|
23160
|
+
const [position, setPosition] = useState(null);
|
|
23161
|
+
const canRemove = state.prefilledHref !== null || uniformLinkHrefOf(doc, state.target) !== null;
|
|
23162
|
+
useLayoutEffect(() => {
|
|
23163
|
+
const input = inputRef.current;
|
|
23164
|
+
if (input === null) return;
|
|
23165
|
+
input.focus();
|
|
23166
|
+
input.select();
|
|
23167
|
+
}, []);
|
|
23168
|
+
useLayoutEffect(() => {
|
|
23169
|
+
const el = popoverRef.current;
|
|
23170
|
+
const root = rootRef.current;
|
|
23171
|
+
if (el === null || root === null) return;
|
|
23172
|
+
const rootRect = root.getBoundingClientRect();
|
|
23173
|
+
const height = el.offsetHeight;
|
|
23174
|
+
const left = clamp(state.anchor.x, ANCHOR_OFFSET_PX, Math.max(ANCHOR_OFFSET_PX, rootRect.width - POPOVER_WIDTH_PX - ANCHOR_OFFSET_PX));
|
|
23175
|
+
const below = state.anchor.y + ANCHOR_OFFSET_PX;
|
|
23176
|
+
const top = below + height > rootRect.height ? Math.max(ANCHOR_OFFSET_PX, state.anchor.y - height - ANCHOR_OFFSET_PX) : below;
|
|
23177
|
+
setPosition({
|
|
23178
|
+
left,
|
|
23179
|
+
top
|
|
23180
|
+
});
|
|
23181
|
+
}, [state.anchor, rootRef]);
|
|
23182
|
+
const apply = () => {
|
|
23183
|
+
const href = resolveLinkHref(value);
|
|
23184
|
+
if (href === null) {
|
|
23185
|
+
setInvalid(true);
|
|
23186
|
+
return;
|
|
23187
|
+
}
|
|
23188
|
+
applyLinkHref(doc, state.target, href, value.trim());
|
|
23189
|
+
onClose();
|
|
23190
|
+
};
|
|
23191
|
+
const remove = () => {
|
|
23192
|
+
removeLink(doc, state.target);
|
|
23193
|
+
onClose();
|
|
23194
|
+
};
|
|
23195
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
23196
|
+
ref: popoverRef,
|
|
23197
|
+
role: "dialog",
|
|
23198
|
+
"data-slot": "docblocks/link-popover",
|
|
23199
|
+
"aria-label": t("link.aria"),
|
|
23200
|
+
className: cn("absolute z-modal rounded-md border border-border bg-popover p-2 text-popover-foreground shadow-md", position === null && "opacity-0"),
|
|
23201
|
+
style: position === null ? { width: POPOVER_WIDTH_PX } : {
|
|
23202
|
+
width: POPOVER_WIDTH_PX,
|
|
23203
|
+
left: position.left,
|
|
23204
|
+
top: position.top
|
|
23205
|
+
},
|
|
23206
|
+
onMouseDown: preventDefault,
|
|
23207
|
+
children: [
|
|
23208
|
+
/* @__PURE__ */ jsx(Input, {
|
|
23209
|
+
ref: inputRef,
|
|
23210
|
+
"data-slot": "docblocks/link-popover-input",
|
|
23211
|
+
size: "sm",
|
|
23212
|
+
value,
|
|
23213
|
+
placeholder: t("link.placeholder"),
|
|
23214
|
+
"aria-invalid": invalid || void 0,
|
|
23215
|
+
onChange: (event) => {
|
|
23216
|
+
setValue(event.target.value);
|
|
23217
|
+
setInvalid(false);
|
|
23218
|
+
},
|
|
23219
|
+
onKeyDown: (event) => {
|
|
23220
|
+
if (event.key === "Enter") {
|
|
23221
|
+
event.preventDefault();
|
|
23222
|
+
event.stopPropagation();
|
|
23223
|
+
apply();
|
|
23224
|
+
} else if (event.key === "Escape") {
|
|
23225
|
+
event.preventDefault();
|
|
23226
|
+
event.stopPropagation();
|
|
23227
|
+
onClose();
|
|
23228
|
+
}
|
|
23229
|
+
}
|
|
23230
|
+
}),
|
|
23231
|
+
invalid && /* @__PURE__ */ jsx("div", {
|
|
23232
|
+
"data-slot": "docblocks/link-popover-invalid",
|
|
23233
|
+
className: "px-2 pt-1 text-xs text-destructive",
|
|
23234
|
+
children: t("link.invalid")
|
|
23235
|
+
}),
|
|
23236
|
+
canRemove && /* @__PURE__ */ jsxs(Button, {
|
|
23237
|
+
"data-slot": "docblocks/link-popover-remove",
|
|
23238
|
+
variant: "ghost",
|
|
23239
|
+
size: "sm",
|
|
23240
|
+
className: "mt-1.5 h-7 w-full justify-start gap-1.5 px-2 text-xs text-destructive",
|
|
23241
|
+
onClick: remove,
|
|
23242
|
+
children: [/* @__PURE__ */ jsx(Trash, { className: "size-3.5" }), t("link.remove")]
|
|
23243
|
+
})
|
|
23244
|
+
]
|
|
23245
|
+
});
|
|
23246
|
+
}
|
|
23247
|
+
//#endregion
|
|
23248
|
+
//#region src/editing/editor-root/history-reseat.ts
|
|
23249
|
+
/**
|
|
23250
|
+
* The history reseat's window over the undo/redo (the remount's transient
|
|
23251
|
+
* selection loss's stand): the window's end stands the mapping's none's
|
|
23252
|
+
* suppression. The engine's remount commit + the selectionchange's settle
|
|
23253
|
+
* stands the sub-second over the load, so the window stands a margin over
|
|
23254
|
+
* it (the staleness is bounded by the window's end; the explicit paths —
|
|
23255
|
+
* the Escape, the gutter's reset — stand the real clear).
|
|
23256
|
+
*/
|
|
23257
|
+
const RESEAT_WINDOW_MS = 2e3;
|
|
23258
|
+
/**
|
|
23259
|
+
* The undo/redo's reseat over the engine's event ordering: the fast path
|
|
23260
|
+
* re-reads the view over the synchronous read (the restoration's stand
|
|
23261
|
+
* lands inside the undo/redo call), and the window's stand re-runs the
|
|
23262
|
+
* reseat when the restoration's emission lands over the transaction's
|
|
23263
|
+
* settle (the engine's ordering stands the popped's event after the
|
|
23264
|
+
* handler's return — the reseat then reads the restored state). The
|
|
23265
|
+
* reseat's own stands (the user's live selection's stop, the seated's
|
|
23266
|
+
* short-circuit) keep the re-runs from clobbering.
|
|
23267
|
+
*/
|
|
23268
|
+
function scheduleHistoryReseat(doc, view, root) {
|
|
23269
|
+
if (view === null) return;
|
|
23270
|
+
const scope = root ?? document;
|
|
23271
|
+
const run = () => {
|
|
23272
|
+
reseatHistorySelection(doc, view, scope);
|
|
23273
|
+
};
|
|
23274
|
+
run();
|
|
23275
|
+
let alive = true;
|
|
23276
|
+
const unsubscribe = view.onChange((selection) => {
|
|
23277
|
+
if (!alive) return;
|
|
23278
|
+
if (selection.kind !== "text") return;
|
|
23279
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => alive && run());
|
|
23280
|
+
else window.setTimeout(() => alive && run(), 0);
|
|
23281
|
+
});
|
|
23282
|
+
window.setTimeout(() => {
|
|
23283
|
+
alive = false;
|
|
23284
|
+
unsubscribe();
|
|
23285
|
+
}, RESEAT_WINDOW_MS);
|
|
23286
|
+
}
|
|
23287
|
+
//#endregion
|
|
23288
|
+
//#region src/editing/link-open.ts
|
|
23289
|
+
/** The link href's open stand (the contenteditable's anchor's gap): the
|
|
23290
|
+
* browser cancels the anchor's navigation default inside a contenteditable
|
|
23291
|
+
* (the caret's seat stands — the mousedown's default), so the editor opens
|
|
23292
|
+
* the href itself. The decision stands the href's class (the core's
|
|
23293
|
+
* `linkHrefKindOf`'s classifier over the normalized form): the web's URL
|
|
23294
|
+
* stands the new tab's open (`noopener,noreferrer` — the gesture's window's
|
|
23295
|
+
* call, the popup's block's off), the `mailto` / `tel` / the relative's
|
|
23296
|
+
* forms stand the same-tab's navigation (the browser's scheme's handler —
|
|
23297
|
+
* the mail client / the dialer — and the relative's page's navigation; the
|
|
23298
|
+
* `window.open`'s call stands no `mailto` in the Chromium family). */
|
|
23299
|
+
/** The open's tab stand over the href's class (the core's classifier —
|
|
23300
|
+
* the web's URL stands the new tab, the other safe forms stand the same
|
|
23301
|
+
* tab); `null` over any href outside the safe contract (the open's
|
|
23302
|
+
* rejection — the anchor reaching the open from a consumer's renderer is
|
|
23303
|
+
* as untrusted as any document content). */
|
|
23304
|
+
const linkOpenTargetOf = (href) => {
|
|
23305
|
+
const kind = linkHrefKindOf(href);
|
|
23306
|
+
if (kind === null) return null;
|
|
23307
|
+
return kind === "web" ? "new-tab" : "same-tab";
|
|
23308
|
+
};
|
|
23309
|
+
/** The href's open over the tab stand (the new tab's `window.open`'s call,
|
|
23310
|
+
* the same tab's `location.assign`'s navigation). An href outside the
|
|
23311
|
+
* safe contract navigates nowhere. */
|
|
23312
|
+
const openLinkHref = (href) => {
|
|
23313
|
+
const target = linkOpenTargetOf(href);
|
|
23314
|
+
if (target === null) return;
|
|
23315
|
+
if (target === "new-tab") window.open(href, "_blank", "noopener,noreferrer");
|
|
23316
|
+
else window.location.assign(href);
|
|
23317
|
+
};
|
|
23318
|
+
//#endregion
|
|
23319
|
+
//#region src/editing/link/use-link-popover.tsx
|
|
23320
|
+
/** The editor-level link popover's wiring: the popover's / the context
|
|
23321
|
+
* menu's state, the opener's api (the toolbar's button / the mod+k's
|
|
23322
|
+
* shortcut / the context menu's edit's item open over it — the target's
|
|
23323
|
+
* resolution + the uniform link's prefill), and the root's `contextmenu`'s
|
|
23324
|
+
* handler (the link's anchor's stand — the native menu's suppress over the
|
|
23325
|
+
* link's own menu; the other's targets stand the native's stand). */
|
|
23326
|
+
/** The selection's rect's bottom to the popover's anchor's gap (px). */
|
|
23327
|
+
const ANCHOR_GAP_PX = 8;
|
|
23328
|
+
/** The selection's reseat's window (the tracking's transient mapping's
|
|
23329
|
+
* suppression over the popover / the menu's open's input's focus and the
|
|
23330
|
+
* close's residue — the doc's selection's no-clobber's stand). */
|
|
23331
|
+
const LINK_RESEAT_WINDOW_MS = RESEAT_WINDOW_MS;
|
|
23332
|
+
/** The anchor element's non-empty text descendants (in document order). */
|
|
23333
|
+
function textNodesOf(anchor) {
|
|
23334
|
+
const nodes = [];
|
|
23335
|
+
const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);
|
|
23336
|
+
let current = walker.nextNode();
|
|
23337
|
+
while (current !== null) {
|
|
23338
|
+
if (current instanceof Text && current.nodeValue !== "") nodes.push(current);
|
|
23339
|
+
current = walker.nextNode();
|
|
23340
|
+
}
|
|
23341
|
+
return nodes;
|
|
23342
|
+
}
|
|
23343
|
+
/** The target's resolution over the view's selection (the toolbar's / the
|
|
23344
|
+
* mod+k's stand): the range over the eligible's target, the caret's link's
|
|
23345
|
+
* expansion over a caret (the inside-a-link's stand, the caret's own
|
|
23346
|
+
* insertion's target otherwise). */
|
|
23347
|
+
function targetOfSelection(doc, selection) {
|
|
23348
|
+
if (selection === null || selection.kind !== "text") return null;
|
|
23349
|
+
if (selection.to !== null) return toolbarRangeOf(doc, selection);
|
|
23350
|
+
return linkTargetOfCaret(doc, selection.from);
|
|
23351
|
+
}
|
|
23352
|
+
function useLinkPopover(rootRef) {
|
|
23353
|
+
const { doc, selection: selectionRef, viewMode } = useEditorContext();
|
|
23354
|
+
const [popover, setPopover] = useState(null);
|
|
23355
|
+
const [menu, setMenu] = useState(null);
|
|
23356
|
+
const suppressTransientSelection = () => {
|
|
23357
|
+
selectionRef.reseatUntil.current = Date.now() + LINK_RESEAT_WINDOW_MS;
|
|
23358
|
+
};
|
|
23359
|
+
const parkFocusRef = useRef(false);
|
|
23360
|
+
const closeAll = useCallback(() => {
|
|
23361
|
+
setPopover(null);
|
|
23362
|
+
setMenu(null);
|
|
23363
|
+
suppressTransientSelection();
|
|
23364
|
+
parkFocusRef.current = true;
|
|
23365
|
+
}, [rootRef, selectionRef]);
|
|
23366
|
+
useEffect(() => {
|
|
23367
|
+
if (!parkFocusRef.current) return;
|
|
23368
|
+
parkFocusRef.current = false;
|
|
23369
|
+
const active = document.activeElement;
|
|
23370
|
+
if (active === null || active === document.body) rootRef.current?.focus();
|
|
23371
|
+
});
|
|
23372
|
+
const open = useCallback((anchor, opts) => {
|
|
23373
|
+
setMenu(null);
|
|
23374
|
+
const view = selectionRef.view;
|
|
23375
|
+
const target = opts?.target ?? targetOfSelection(doc, view !== null ? view.get() : null);
|
|
23376
|
+
if (target === null) return;
|
|
23377
|
+
const prefilledHref = opts?.prefilledHref ?? uniformLinkHrefOf(doc, target);
|
|
23378
|
+
setPopover({
|
|
23379
|
+
anchor,
|
|
23380
|
+
target,
|
|
23381
|
+
prefilledHref
|
|
23382
|
+
});
|
|
23383
|
+
suppressTransientSelection();
|
|
23384
|
+
}, [doc, selectionRef]);
|
|
23385
|
+
const openFromSelection = useCallback((selection) => {
|
|
23386
|
+
const target = targetOfSelection(doc, selection);
|
|
23387
|
+
if (target === null) return;
|
|
23388
|
+
const root = rootRef.current;
|
|
23389
|
+
let anchor = {
|
|
23390
|
+
x: 8,
|
|
23391
|
+
y: 8
|
|
23392
|
+
};
|
|
23393
|
+
const domSelection = window.getSelection();
|
|
23394
|
+
const range = domSelection !== null && domSelection.rangeCount > 0 ? domSelection.getRangeAt(0) : null;
|
|
23395
|
+
if (root !== null && range !== null && typeof range.getBoundingClientRect === "function") {
|
|
23396
|
+
const rect = range.getBoundingClientRect();
|
|
23397
|
+
const rootRect = root.getBoundingClientRect();
|
|
23398
|
+
anchor = {
|
|
23399
|
+
x: rect.left + rect.width / 2 - rootRect.left,
|
|
23400
|
+
y: rect.bottom - rootRect.top + ANCHOR_GAP_PX
|
|
23401
|
+
};
|
|
23402
|
+
}
|
|
23403
|
+
open(anchor, { target });
|
|
23404
|
+
}, [
|
|
23405
|
+
doc,
|
|
23406
|
+
open,
|
|
23407
|
+
rootRef
|
|
23408
|
+
]);
|
|
23409
|
+
const api = useMemo(() => ({
|
|
23410
|
+
open,
|
|
23411
|
+
openFromSelection
|
|
23412
|
+
}), [open, openFromSelection]);
|
|
23413
|
+
/** The mod+k's interception (the read-only's / the nested editor's /
|
|
23414
|
+
* the form control's stands pass through): the popover's open over the
|
|
23415
|
+
* current view's selection, the no-op's stand over a non-text's view. */
|
|
23416
|
+
const handleKeyDown = useCallback((event) => {
|
|
23417
|
+
if (viewMode) return false;
|
|
23418
|
+
if (!(event.metaKey || event.ctrlKey) || event.shiftKey || event.key.toLowerCase() !== "k") return false;
|
|
23419
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
23420
|
+
if (target !== null && target.closest("input, textarea") !== null) return false;
|
|
23421
|
+
const root = rootRef.current;
|
|
23422
|
+
const formControl = target?.closest("[contenteditable='true']") ?? null;
|
|
23423
|
+
if (root !== null && target !== null && formControl !== null && !root.contains(target)) return false;
|
|
23424
|
+
const nearest = target?.closest("[data-slot=\"docblocks/editor\"]") ?? null;
|
|
23425
|
+
if (root !== null && nearest !== null && nearest !== root) return false;
|
|
23426
|
+
event.preventDefault();
|
|
23427
|
+
const view = selectionRef.view;
|
|
23428
|
+
openFromSelection(view !== null ? view.get() : null);
|
|
23429
|
+
return true;
|
|
23430
|
+
}, [
|
|
23431
|
+
viewMode,
|
|
23432
|
+
rootRef,
|
|
23433
|
+
selectionRef,
|
|
23434
|
+
openFromSelection
|
|
23435
|
+
]);
|
|
23436
|
+
/** The root's contextmenu: over the link's anchor — the link's menu (the
|
|
23437
|
+
* native's suppress); otherwise — the native's stand (the table's cell's
|
|
23438
|
+
* menu / the browser's own stand over the other's targets). */
|
|
23439
|
+
const onContextMenu = useCallback((event) => {
|
|
23440
|
+
const anchorEl = (event.target instanceof Element ? event.target : null)?.closest("a[data-slot='docblocks/link']") ?? null;
|
|
23441
|
+
const root = rootRef.current;
|
|
23442
|
+
if (anchorEl === null || root === null) return;
|
|
23443
|
+
const href = anchorEl.getAttribute("href");
|
|
23444
|
+
const blockId = anchorEl.closest("[data-block-id]")?.dataset.blockId;
|
|
23445
|
+
if (href === null || blockId === void 0) return;
|
|
23446
|
+
const inner = blockInnerOf(rootRef, blockId);
|
|
23447
|
+
const nodes = inner !== null ? textNodesOf(anchorEl) : [];
|
|
23448
|
+
if (inner === null || nodes.length === 0) return;
|
|
23449
|
+
const from = caretOffsetIn(inner, nodes[0], 0);
|
|
23450
|
+
const last = nodes[nodes.length - 1];
|
|
23451
|
+
const to = caretOffsetIn(inner, last, last.nodeValue?.length ?? 0);
|
|
23452
|
+
if (from >= to) return;
|
|
23453
|
+
event.preventDefault();
|
|
23454
|
+
setPopover(null);
|
|
23455
|
+
setMenu({
|
|
23456
|
+
x: event.clientX,
|
|
23457
|
+
y: event.clientY,
|
|
23458
|
+
href,
|
|
23459
|
+
blockId,
|
|
23460
|
+
from,
|
|
23461
|
+
to
|
|
23462
|
+
});
|
|
23463
|
+
}, [rootRef]);
|
|
23464
|
+
const handleOpen = useCallback(() => {
|
|
23465
|
+
if (menu !== null) {
|
|
23466
|
+
openLinkHref(menu.href);
|
|
23467
|
+
closeAll();
|
|
23468
|
+
}
|
|
23469
|
+
}, [closeAll, menu]);
|
|
23470
|
+
const handleCopy = useCallback(() => {
|
|
23471
|
+
if (menu === null) return;
|
|
23472
|
+
try {
|
|
23473
|
+
navigator.clipboard.writeText(menu.href);
|
|
23474
|
+
} catch {}
|
|
23475
|
+
closeAll();
|
|
23476
|
+
}, [closeAll, menu]);
|
|
23477
|
+
const handleEdit = useCallback(() => {
|
|
23478
|
+
if (menu === null) return;
|
|
23479
|
+
const root = rootRef.current;
|
|
23480
|
+
if (root === null) return;
|
|
23481
|
+
const rootRect = root.getBoundingClientRect();
|
|
23482
|
+
open({
|
|
23483
|
+
x: menu.x - rootRect.left,
|
|
23484
|
+
y: menu.y - rootRect.top
|
|
23485
|
+
}, {
|
|
23486
|
+
target: {
|
|
23487
|
+
kind: "single",
|
|
23488
|
+
blockId: menu.blockId,
|
|
23489
|
+
from: menu.from,
|
|
23490
|
+
to: menu.to
|
|
23491
|
+
},
|
|
23492
|
+
prefilledHref: menu.href
|
|
23493
|
+
});
|
|
23494
|
+
}, [
|
|
23495
|
+
menu,
|
|
23496
|
+
open,
|
|
23497
|
+
rootRef
|
|
23498
|
+
]);
|
|
23499
|
+
const handleRemove = useCallback(() => {
|
|
23500
|
+
if (menu === null) return;
|
|
23501
|
+
removeLink(doc, {
|
|
23502
|
+
kind: "single",
|
|
23503
|
+
blockId: menu.blockId,
|
|
23504
|
+
from: menu.from,
|
|
23505
|
+
to: menu.to
|
|
23506
|
+
});
|
|
23507
|
+
closeAll();
|
|
23508
|
+
}, [
|
|
23509
|
+
closeAll,
|
|
23510
|
+
doc,
|
|
23511
|
+
menu
|
|
23512
|
+
]);
|
|
23513
|
+
return {
|
|
23514
|
+
api,
|
|
23515
|
+
handleKeyDown,
|
|
23516
|
+
onContextMenu,
|
|
23517
|
+
node: /* @__PURE__ */ jsxs(Fragment$1, { children: [menu !== null && /* @__PURE__ */ jsx(LinkMenu, {
|
|
23518
|
+
menu,
|
|
23519
|
+
viewMode,
|
|
23520
|
+
onOpen: handleOpen,
|
|
23521
|
+
onCopy: handleCopy,
|
|
23522
|
+
onEdit: handleEdit,
|
|
23523
|
+
onRemove: handleRemove,
|
|
23524
|
+
onClose: closeAll
|
|
23525
|
+
}), popover !== null && /* @__PURE__ */ jsx(LinkPopover, {
|
|
23526
|
+
rootRef,
|
|
23527
|
+
state: popover,
|
|
23528
|
+
onClose: closeAll
|
|
23529
|
+
})] })
|
|
23530
|
+
};
|
|
23531
|
+
}
|
|
23532
|
+
//#endregion
|
|
22739
23533
|
//#region src/editing/format-toolbar/FormatListMenu.tsx
|
|
22740
23534
|
/** The format toolbar's LIST-TYPE menu (single-block paragraph / heading /
|
|
22741
23535
|
* list-item targets only): a non-modal dropdown — a radio group that turns
|
|
@@ -22947,6 +23741,7 @@ function createFormatToolbarActions({ doc, range, single, target, rootRef, onPal
|
|
|
22947
23741
|
const alignActive = (align) => range.kind === "cross" ? uniformAlignOfRange(doc, range.cross) === align : alignOfBlock(single) === align;
|
|
22948
23742
|
const paletteActive = (value) => range.kind === "cross" ? uniformMarkValueRange(doc, range.cross, target) === value : uniformMarkValue(single, range.from, range.to, target) === value;
|
|
22949
23743
|
const currentMarkValue = range.kind === "cross" ? uniformMarkValueRange(doc, range.cross, target) : uniformMarkValue(single, range.from, range.to, target);
|
|
23744
|
+
const linkUniformHref = uniformLinkHrefOf(doc, range);
|
|
22950
23745
|
return {
|
|
22951
23746
|
onToggleMark,
|
|
22952
23747
|
onAlign,
|
|
@@ -22956,7 +23751,8 @@ function createFormatToolbarActions({ doc, range, single, target, rootRef, onPal
|
|
|
22956
23751
|
alignActive,
|
|
22957
23752
|
paletteActive,
|
|
22958
23753
|
currentMarkValue,
|
|
22959
|
-
chipValue: currentMarkValue !== null && currentMarkValue !== "mixed" ? currentMarkValue : null
|
|
23754
|
+
chipValue: currentMarkValue !== null && currentMarkValue !== "mixed" ? currentMarkValue : null,
|
|
23755
|
+
linkUniformHref
|
|
22960
23756
|
};
|
|
22961
23757
|
}
|
|
22962
23758
|
//#endregion
|
|
@@ -22988,27 +23784,12 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
22988
23784
|
const { t } = useTranslation("editor");
|
|
22989
23785
|
const { doc } = useEditorContext();
|
|
22990
23786
|
const selection = useSelection();
|
|
23787
|
+
const linkApi = useLinkPopoverApi();
|
|
22991
23788
|
const barRef = useRef(null);
|
|
22992
23789
|
const [paletteOpen, setPaletteOpen] = useState(false);
|
|
22993
23790
|
const [listMenuOpen, setListMenuOpen] = useState(false);
|
|
22994
23791
|
const [target, setTarget] = useState("highlight");
|
|
22995
|
-
const range = useMemo(() =>
|
|
22996
|
-
if (selection.kind !== "text" || selection.to === null) return null;
|
|
22997
|
-
if (selection.from.blockId === selection.to.blockId) {
|
|
22998
|
-
if (selection.from.offset === selection.to.offset) return null;
|
|
22999
|
-
return {
|
|
23000
|
-
kind: "single",
|
|
23001
|
-
blockId: selection.from.blockId,
|
|
23002
|
-
from: selection.from.offset,
|
|
23003
|
-
to: selection.to.offset
|
|
23004
|
-
};
|
|
23005
|
-
}
|
|
23006
|
-
const cross = crossRangeOf(selection);
|
|
23007
|
-
return cross !== null && crossRangeEligible(doc, cross) ? {
|
|
23008
|
-
kind: "cross",
|
|
23009
|
-
cross
|
|
23010
|
-
} : null;
|
|
23011
|
-
}, [doc, selection]);
|
|
23792
|
+
const range = useMemo(() => toolbarRangeOf(doc, selection), [doc, selection]);
|
|
23012
23793
|
const block = useBlock(range !== null && range.kind === "single" ? range.blockId : "");
|
|
23013
23794
|
const selectionKey = selection.kind === "text" ? `${selection.from.blockId}:${selection.from.offset}:${selection.to?.blockId ?? ""}:${selection.to?.offset ?? "caret"}` : selection.kind;
|
|
23014
23795
|
useEffect(() => {
|
|
@@ -23048,6 +23829,17 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
23048
23829
|
onListMenuClose: () => setListMenuOpen(false)
|
|
23049
23830
|
});
|
|
23050
23831
|
const listValue = block !== void 0 && block.type === "list-item" ? block.attrs.list : block !== void 0 && block.type === "paragraph" ? "paragraph" : block !== void 0 && block.type === "quote" ? "quote" : "";
|
|
23832
|
+
const onLinkClick = () => {
|
|
23833
|
+
const bar = barRef.current;
|
|
23834
|
+
const root = rootRef.current;
|
|
23835
|
+
if (bar === null || root === null || range === null || linkApi === null) return;
|
|
23836
|
+
const barRect = bar.getBoundingClientRect();
|
|
23837
|
+
const rootRect = root.getBoundingClientRect();
|
|
23838
|
+
linkApi.open({
|
|
23839
|
+
x: barRect.left - rootRect.left,
|
|
23840
|
+
y: barRect.bottom - rootRect.top + 4
|
|
23841
|
+
}, { target: range });
|
|
23842
|
+
};
|
|
23051
23843
|
return /* @__PURE__ */ jsxs("div", {
|
|
23052
23844
|
"data-slot": "docblocks/format-toolbar",
|
|
23053
23845
|
ref: barRef,
|
|
@@ -23067,6 +23859,17 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
23067
23859
|
onClick: () => actions.onToggleMark(mark),
|
|
23068
23860
|
children: /* @__PURE__ */ jsx(Icon, { className: "size-4" })
|
|
23069
23861
|
}, mark)),
|
|
23862
|
+
/* @__PURE__ */ jsx(Button, {
|
|
23863
|
+
variant: "ghost",
|
|
23864
|
+
size: "icon",
|
|
23865
|
+
"data-link-btn": true,
|
|
23866
|
+
title: t("formatToolbar.marks.link"),
|
|
23867
|
+
"aria-label": t("formatToolbar.marks.link"),
|
|
23868
|
+
"aria-pressed": actions.linkUniformHref !== null,
|
|
23869
|
+
className: cn(actions.linkUniformHref !== null && "bg-accent text-accent-foreground"),
|
|
23870
|
+
onClick: onLinkClick,
|
|
23871
|
+
children: /* @__PURE__ */ jsx(Link, { className: "size-4" })
|
|
23872
|
+
}),
|
|
23070
23873
|
/* @__PURE__ */ jsx(Separator, {
|
|
23071
23874
|
orientation: "vertical",
|
|
23072
23875
|
className: "h-5"
|
|
@@ -28884,6 +29687,7 @@ function useTableCellMenu({ doc, viewMode, frameRef }) {
|
|
|
28884
29687
|
*/
|
|
28885
29688
|
const onTableContextMenu = (event) => {
|
|
28886
29689
|
if (viewMode) return;
|
|
29690
|
+
if (event.target.closest?.("a[data-slot='docblocks/link']") !== null) return;
|
|
28887
29691
|
const cell = event.target.closest("td[data-block-id]");
|
|
28888
29692
|
if (cell === null) return;
|
|
28889
29693
|
const cellId = cell.dataset.blockId ?? "";
|
|
@@ -35819,46 +36623,6 @@ function handleDocumentEdge(event, deps) {
|
|
|
35819
36623
|
return true;
|
|
35820
36624
|
}
|
|
35821
36625
|
//#endregion
|
|
35822
|
-
//#region src/editing/editor-root/history-reseat.ts
|
|
35823
|
-
/**
|
|
35824
|
-
* The history reseat's window over the undo/redo (the remount's transient
|
|
35825
|
-
* selection loss's stand): the window's end stands the mapping's none's
|
|
35826
|
-
* suppression. The engine's remount commit + the selectionchange's settle
|
|
35827
|
-
* stands the sub-second over the load, so the window stands a margin over
|
|
35828
|
-
* it (the staleness is bounded by the window's end; the explicit paths —
|
|
35829
|
-
* the Escape, the gutter's reset — stand the real clear).
|
|
35830
|
-
*/
|
|
35831
|
-
const RESEAT_WINDOW_MS = 2e3;
|
|
35832
|
-
/**
|
|
35833
|
-
* The undo/redo's reseat over the engine's event ordering: the fast path
|
|
35834
|
-
* re-reads the view over the synchronous read (the restoration's stand
|
|
35835
|
-
* lands inside the undo/redo call), and the window's stand re-runs the
|
|
35836
|
-
* reseat when the restoration's emission lands over the transaction's
|
|
35837
|
-
* settle (the engine's ordering stands the popped's event after the
|
|
35838
|
-
* handler's return — the reseat then reads the restored state). The
|
|
35839
|
-
* reseat's own stands (the user's live selection's stop, the seated's
|
|
35840
|
-
* short-circuit) keep the re-runs from clobbering.
|
|
35841
|
-
*/
|
|
35842
|
-
function scheduleHistoryReseat(doc, view, root) {
|
|
35843
|
-
if (view === null) return;
|
|
35844
|
-
const scope = root ?? document;
|
|
35845
|
-
const run = () => {
|
|
35846
|
-
reseatHistorySelection(doc, view, scope);
|
|
35847
|
-
};
|
|
35848
|
-
run();
|
|
35849
|
-
let alive = true;
|
|
35850
|
-
const unsubscribe = view.onChange((selection) => {
|
|
35851
|
-
if (!alive) return;
|
|
35852
|
-
if (selection.kind !== "text") return;
|
|
35853
|
-
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => alive && run());
|
|
35854
|
-
else window.setTimeout(() => alive && run(), 0);
|
|
35855
|
-
});
|
|
35856
|
-
window.setTimeout(() => {
|
|
35857
|
-
alive = false;
|
|
35858
|
-
unsubscribe();
|
|
35859
|
-
}, RESEAT_WINDOW_MS);
|
|
35860
|
-
}
|
|
35861
|
-
//#endregion
|
|
35862
36626
|
//#region src/editing/editor-root/selection-tracking.ts
|
|
35863
36627
|
/** The editor root's `selectionchange` mapping: the DOM selection → the
|
|
35864
36628
|
* core Selection view (the editing stand's tracking). */
|
|
@@ -35881,7 +36645,10 @@ function useSelectionTracking(doc, rootRef, selectionRef, viewMode) {
|
|
|
35881
36645
|
return;
|
|
35882
36646
|
}
|
|
35883
36647
|
const next = mapDomSelection(root, window.getSelection(), doc);
|
|
35884
|
-
|
|
36648
|
+
const current = view.get();
|
|
36649
|
+
const linkAnchor = window.getSelection()?.anchorNode ?? null;
|
|
36650
|
+
if ((linkAnchor instanceof Element ? linkAnchor : linkAnchor?.parentElement ?? null)?.closest("[data-slot='docblocks/link-popover'], [data-slot='docblocks/link-menu']") !== null) return;
|
|
36651
|
+
if (current.kind === "blocks" && !domSelectionInEditable(window.getSelection())) return;
|
|
35885
36652
|
const anchorNode = window.getSelection()?.anchorNode ?? null;
|
|
35886
36653
|
if ((anchorNode instanceof Element ? anchorNode : anchorNode?.parentElement ?? null)?.closest("[data-slot=\"docblocks/page-card\"]") !== null) return;
|
|
35887
36654
|
if (next.kind !== "text" && Date.now() < selectionRef.reseatUntil.current) return;
|
|
@@ -35995,7 +36762,7 @@ function useLinkOpen() {
|
|
|
35995
36762
|
const target = event.target;
|
|
35996
36763
|
const anchor = target instanceof Element ? target.closest("a[href]") : null;
|
|
35997
36764
|
const href = anchor !== null ? anchor.getAttribute("href") : null;
|
|
35998
|
-
pressRef.current = anchor !== null && href !== null &&
|
|
36765
|
+
pressRef.current = anchor !== null && href !== null && linkHrefKindOf(href) !== null ? {
|
|
35999
36766
|
href,
|
|
36000
36767
|
x: event.clientX,
|
|
36001
36768
|
y: event.clientY
|
|
@@ -36009,7 +36776,7 @@ function useLinkOpen() {
|
|
|
36009
36776
|
const dy = event.clientY - press.y;
|
|
36010
36777
|
if (dx * dx + dy * dy > 16) return;
|
|
36011
36778
|
event.preventDefault();
|
|
36012
|
-
|
|
36779
|
+
openLinkHref(press.href);
|
|
36013
36780
|
}, [])
|
|
36014
36781
|
};
|
|
36015
36782
|
}
|
|
@@ -36997,6 +37764,11 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
36997
37764
|
rootChildren
|
|
36998
37765
|
]);
|
|
36999
37766
|
const { rootRef, onKeyDown, onCopy, onPointerDown: onCrossDragPointerDown } = useEditing();
|
|
37767
|
+
const linkPopover = useLinkPopover(rootRef);
|
|
37768
|
+
const handleKeyDown = (event) => {
|
|
37769
|
+
if (linkPopover.handleKeyDown(event)) return;
|
|
37770
|
+
onKeyDown(event);
|
|
37771
|
+
};
|
|
37000
37772
|
const linkOpen = useLinkOpen();
|
|
37001
37773
|
const slash = useSlashMenu(rootRef);
|
|
37002
37774
|
const mention = useMentionMenu(rootRef);
|
|
@@ -37062,8 +37834,9 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
37062
37834
|
"data-mention-interactive": mentionInteractions !== null ? "true" : void 0,
|
|
37063
37835
|
tabIndex: -1,
|
|
37064
37836
|
className: cn("relative min-h-0 flex-1 selection:bg-(--docblocks-selection-background)", "font-(family-name:--docblocks-font-family) text-(length:--docblocks-font-size)", "leading-(--docblocks-line-height) text-(--docblocks-text-color)"),
|
|
37065
|
-
onKeyDown,
|
|
37837
|
+
onKeyDown: handleKeyDown,
|
|
37066
37838
|
onCopy,
|
|
37839
|
+
onContextMenu: linkPopover.onContextMenu,
|
|
37067
37840
|
onMouseDown: linkOpen.onMouseDown,
|
|
37068
37841
|
onClick: linkOpen.onClick,
|
|
37069
37842
|
onPointerDown: onCrossDragPointerDown,
|
|
@@ -37110,7 +37883,11 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
37110
37883
|
slash.menuNode,
|
|
37111
37884
|
mention.menuNode,
|
|
37112
37885
|
mentionCard,
|
|
37113
|
-
|
|
37886
|
+
/* @__PURE__ */ jsx(LinkPopoverContext.Provider, {
|
|
37887
|
+
value: linkPopover.api,
|
|
37888
|
+
children: viewMode ? null : format.toolbarNode
|
|
37889
|
+
}),
|
|
37890
|
+
linkPopover.node
|
|
37114
37891
|
]
|
|
37115
37892
|
})
|
|
37116
37893
|
})]
|