docblocks-react-ui 0.15.0 → 0.16.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 +5 -1
- package/dist/{Editor-BF7wBMcp.mjs → Editor-BrENM2MV.mjs} +833 -71
- 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,646 @@ 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 leading `/` / `#` / `.` stands
|
|
23096
|
+
* the relative's form; the email's form (the `@` without the whitespace)
|
|
23097
|
+
* stands the `mailto:`; the phone's form (the digits + the separators, no
|
|
23098
|
+
* letters) stands the `tel:`; the bare's form (a domain / a path's text)
|
|
23099
|
+
* stands the `https://`'s prefix. `null` stands the rejection (the
|
|
23100
|
+
* popover's invalid's stand). */
|
|
23101
|
+
/** The explicit scheme's prefix (the `mailto:` / the `tel:` / the web's
|
|
23102
|
+
* URL's discriminator — the scheme's case-insensitive stand). */
|
|
23103
|
+
const SCHEME_PREFIX = /^[a-z][a-z0-9+.-]*:/i;
|
|
23104
|
+
/** The phone's form: 5–20 characters of digits and the phone's separators
|
|
23105
|
+
* (no letters) — the versions' / the dates' tokens stand the https's
|
|
23106
|
+
* form over it (the digits' minimum is checked over the call). */
|
|
23107
|
+
const PHONE_FORM = /^[0-9()+\-'., ]{5,20}$/;
|
|
23108
|
+
/** The minimum digit count of the phone's form (the `tel:`'s payload's
|
|
23109
|
+
* parity over the core's contract). */
|
|
23110
|
+
const PHONE_MIN_DIGITS = 5;
|
|
23111
|
+
function resolveLinkHref(input) {
|
|
23112
|
+
const value = input.trim();
|
|
23113
|
+
if (value === "") return null;
|
|
23114
|
+
if (SCHEME_PREFIX.test(value)) return isSafeLinkHref(value) ? value : null;
|
|
23115
|
+
if (value.startsWith("/") || value.startsWith("#") || value.startsWith(".")) return isSafeLinkHref(value) ? value : null;
|
|
23116
|
+
if (value.includes("@") && !/\s/.test(value)) {
|
|
23117
|
+
const href = `mailto:${value}`;
|
|
23118
|
+
return isSafeLinkHref(href) ? href : null;
|
|
23119
|
+
}
|
|
23120
|
+
if (PHONE_FORM.test(value) && (value.match(/\d/g) ?? []).length >= PHONE_MIN_DIGITS) {
|
|
23121
|
+
const href = `tel:${value}`;
|
|
23122
|
+
return isSafeLinkHref(href) ? href : null;
|
|
23123
|
+
}
|
|
23124
|
+
if (/\s/.test(value)) return null;
|
|
23125
|
+
const web = `https://${value}`;
|
|
23126
|
+
return isSafeLinkHref(web) ? web : null;
|
|
23127
|
+
}
|
|
23128
|
+
//#endregion
|
|
23129
|
+
//#region src/editing/link/LinkPopover.tsx
|
|
23130
|
+
/** The link editor's floating popover: the one field (the address's input
|
|
23131
|
+
* over the smart scheme's inference — the `resolve`'s unit) + the
|
|
23132
|
+
* remove's button (over the linked's target's stand). A plain
|
|
23133
|
+
* absolutely-positioned div inside the editor root (the root is the
|
|
23134
|
+
* positioning context), like the slash / the mention's menus. Enter / the
|
|
23135
|
+
* apply stands the commit (the `applyLinkHref`'s one transaction), the
|
|
23136
|
+
* Escape stands the close (the no-write's stand), the invalid's stand
|
|
23137
|
+
* stands the destructive's border + the message. The popover's mousedown
|
|
23138
|
+
* stands the default's suppress (the selection's life over the edit).
|
|
23139
|
+
*/
|
|
23140
|
+
/** The popover's width (px — the input's field's stand). */
|
|
23141
|
+
const POPOVER_WIDTH_PX = 288;
|
|
23142
|
+
/** The anchor's offset from the popover's edge (px — the flip's margin). */
|
|
23143
|
+
const ANCHOR_OFFSET_PX = 8;
|
|
23144
|
+
function LinkPopover({ rootRef, state, onClose }) {
|
|
23145
|
+
const { t } = useTranslation("editor");
|
|
23146
|
+
const { doc } = useEditorContext();
|
|
23147
|
+
const popoverRef = useRef(null);
|
|
23148
|
+
const inputRef = useRef(null);
|
|
23149
|
+
const [value, setValue] = useState(state.prefilledHref ?? "");
|
|
23150
|
+
const [invalid, setInvalid] = useState(false);
|
|
23151
|
+
const [position, setPosition] = useState(null);
|
|
23152
|
+
const canRemove = state.prefilledHref !== null || uniformLinkHrefOf(doc, state.target) !== null;
|
|
23153
|
+
useLayoutEffect(() => {
|
|
23154
|
+
const input = inputRef.current;
|
|
23155
|
+
if (input === null) return;
|
|
23156
|
+
input.focus();
|
|
23157
|
+
input.select();
|
|
23158
|
+
}, []);
|
|
23159
|
+
useLayoutEffect(() => {
|
|
23160
|
+
const el = popoverRef.current;
|
|
23161
|
+
const root = rootRef.current;
|
|
23162
|
+
if (el === null || root === null) return;
|
|
23163
|
+
const rootRect = root.getBoundingClientRect();
|
|
23164
|
+
const height = el.offsetHeight;
|
|
23165
|
+
const left = clamp(state.anchor.x, ANCHOR_OFFSET_PX, Math.max(ANCHOR_OFFSET_PX, rootRect.width - POPOVER_WIDTH_PX - ANCHOR_OFFSET_PX));
|
|
23166
|
+
const below = state.anchor.y + ANCHOR_OFFSET_PX;
|
|
23167
|
+
const top = below + height > rootRect.height ? Math.max(ANCHOR_OFFSET_PX, state.anchor.y - height - ANCHOR_OFFSET_PX) : below;
|
|
23168
|
+
setPosition({
|
|
23169
|
+
left,
|
|
23170
|
+
top
|
|
23171
|
+
});
|
|
23172
|
+
}, [state.anchor, rootRef]);
|
|
23173
|
+
const apply = () => {
|
|
23174
|
+
const href = resolveLinkHref(value);
|
|
23175
|
+
if (href === null) {
|
|
23176
|
+
setInvalid(true);
|
|
23177
|
+
return;
|
|
23178
|
+
}
|
|
23179
|
+
applyLinkHref(doc, state.target, href, value.trim());
|
|
23180
|
+
onClose();
|
|
23181
|
+
};
|
|
23182
|
+
const remove = () => {
|
|
23183
|
+
removeLink(doc, state.target);
|
|
23184
|
+
onClose();
|
|
23185
|
+
};
|
|
23186
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
23187
|
+
ref: popoverRef,
|
|
23188
|
+
role: "dialog",
|
|
23189
|
+
"data-slot": "docblocks/link-popover",
|
|
23190
|
+
"aria-label": t("link.aria"),
|
|
23191
|
+
className: cn("absolute z-modal rounded-md border border-border bg-popover p-2 text-popover-foreground shadow-md", position === null && "opacity-0"),
|
|
23192
|
+
style: position === null ? { width: POPOVER_WIDTH_PX } : {
|
|
23193
|
+
width: POPOVER_WIDTH_PX,
|
|
23194
|
+
left: position.left,
|
|
23195
|
+
top: position.top
|
|
23196
|
+
},
|
|
23197
|
+
onMouseDown: preventDefault,
|
|
23198
|
+
children: [
|
|
23199
|
+
/* @__PURE__ */ jsx(Input, {
|
|
23200
|
+
ref: inputRef,
|
|
23201
|
+
"data-slot": "docblocks/link-popover-input",
|
|
23202
|
+
size: "sm",
|
|
23203
|
+
value,
|
|
23204
|
+
placeholder: t("link.placeholder"),
|
|
23205
|
+
"aria-invalid": invalid || void 0,
|
|
23206
|
+
onChange: (event) => {
|
|
23207
|
+
setValue(event.target.value);
|
|
23208
|
+
setInvalid(false);
|
|
23209
|
+
},
|
|
23210
|
+
onKeyDown: (event) => {
|
|
23211
|
+
if (event.key === "Enter") {
|
|
23212
|
+
event.preventDefault();
|
|
23213
|
+
event.stopPropagation();
|
|
23214
|
+
apply();
|
|
23215
|
+
} else if (event.key === "Escape") {
|
|
23216
|
+
event.preventDefault();
|
|
23217
|
+
event.stopPropagation();
|
|
23218
|
+
onClose();
|
|
23219
|
+
}
|
|
23220
|
+
}
|
|
23221
|
+
}),
|
|
23222
|
+
invalid && /* @__PURE__ */ jsx("div", {
|
|
23223
|
+
"data-slot": "docblocks/link-popover-invalid",
|
|
23224
|
+
className: "px-2 pt-1 text-xs text-destructive",
|
|
23225
|
+
children: t("link.invalid")
|
|
23226
|
+
}),
|
|
23227
|
+
canRemove && /* @__PURE__ */ jsxs(Button, {
|
|
23228
|
+
"data-slot": "docblocks/link-popover-remove",
|
|
23229
|
+
variant: "ghost",
|
|
23230
|
+
size: "sm",
|
|
23231
|
+
className: "mt-1.5 h-7 w-full justify-start gap-1.5 px-2 text-xs text-destructive",
|
|
23232
|
+
onClick: remove,
|
|
23233
|
+
children: [/* @__PURE__ */ jsx(Trash, { className: "size-3.5" }), t("link.remove")]
|
|
23234
|
+
})
|
|
23235
|
+
]
|
|
23236
|
+
});
|
|
23237
|
+
}
|
|
23238
|
+
//#endregion
|
|
23239
|
+
//#region src/editing/editor-root/history-reseat.ts
|
|
23240
|
+
/**
|
|
23241
|
+
* The history reseat's window over the undo/redo (the remount's transient
|
|
23242
|
+
* selection loss's stand): the window's end stands the mapping's none's
|
|
23243
|
+
* suppression. The engine's remount commit + the selectionchange's settle
|
|
23244
|
+
* stands the sub-second over the load, so the window stands a margin over
|
|
23245
|
+
* it (the staleness is bounded by the window's end; the explicit paths —
|
|
23246
|
+
* the Escape, the gutter's reset — stand the real clear).
|
|
23247
|
+
*/
|
|
23248
|
+
const RESEAT_WINDOW_MS = 2e3;
|
|
23249
|
+
/**
|
|
23250
|
+
* The undo/redo's reseat over the engine's event ordering: the fast path
|
|
23251
|
+
* re-reads the view over the synchronous read (the restoration's stand
|
|
23252
|
+
* lands inside the undo/redo call), and the window's stand re-runs the
|
|
23253
|
+
* reseat when the restoration's emission lands over the transaction's
|
|
23254
|
+
* settle (the engine's ordering stands the popped's event after the
|
|
23255
|
+
* handler's return — the reseat then reads the restored state). The
|
|
23256
|
+
* reseat's own stands (the user's live selection's stop, the seated's
|
|
23257
|
+
* short-circuit) keep the re-runs from clobbering.
|
|
23258
|
+
*/
|
|
23259
|
+
function scheduleHistoryReseat(doc, view, root) {
|
|
23260
|
+
if (view === null) return;
|
|
23261
|
+
const scope = root ?? document;
|
|
23262
|
+
const run = () => {
|
|
23263
|
+
reseatHistorySelection(doc, view, scope);
|
|
23264
|
+
};
|
|
23265
|
+
run();
|
|
23266
|
+
let alive = true;
|
|
23267
|
+
const unsubscribe = view.onChange((selection) => {
|
|
23268
|
+
if (!alive) return;
|
|
23269
|
+
if (selection.kind !== "text") return;
|
|
23270
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => alive && run());
|
|
23271
|
+
else window.setTimeout(() => alive && run(), 0);
|
|
23272
|
+
});
|
|
23273
|
+
window.setTimeout(() => {
|
|
23274
|
+
alive = false;
|
|
23275
|
+
unsubscribe();
|
|
23276
|
+
}, RESEAT_WINDOW_MS);
|
|
23277
|
+
}
|
|
23278
|
+
//#endregion
|
|
23279
|
+
//#region src/editing/link-open.ts
|
|
23280
|
+
/** The link href's open stand (the contenteditable's anchor's gap): the
|
|
23281
|
+
* browser cancels the anchor's navigation default inside a contenteditable
|
|
23282
|
+
* (the caret's seat stands — the mousedown's default), so the editor opens
|
|
23283
|
+
* the href itself. The decision stands the href's class (the core's
|
|
23284
|
+
* `linkHrefKindOf`'s classifier over the normalized form): the web's URL
|
|
23285
|
+
* stands the new tab's open (`noopener,noreferrer` — the gesture's window's
|
|
23286
|
+
* call, the popup's block's off), the `mailto` / `tel` / the relative's
|
|
23287
|
+
* forms stand the same-tab's navigation (the browser's scheme's handler —
|
|
23288
|
+
* the mail client / the dialer — and the relative's page's navigation; the
|
|
23289
|
+
* `window.open`'s call stands no `mailto` in the Chromium family). */
|
|
23290
|
+
/** The open's tab stand over the href's class (the core's classifier —
|
|
23291
|
+
* the web's URL stands the new tab, the other safe forms stand the same
|
|
23292
|
+
* tab); `null` over any href outside the safe contract (the open's
|
|
23293
|
+
* rejection — the anchor reaching the open from a consumer's renderer is
|
|
23294
|
+
* as untrusted as any document content). */
|
|
23295
|
+
const linkOpenTargetOf = (href) => {
|
|
23296
|
+
const kind = linkHrefKindOf(href);
|
|
23297
|
+
if (kind === null) return null;
|
|
23298
|
+
return kind === "web" ? "new-tab" : "same-tab";
|
|
23299
|
+
};
|
|
23300
|
+
/** The href's open over the tab stand (the new tab's `window.open`'s call,
|
|
23301
|
+
* the same tab's `location.assign`'s navigation). An href outside the
|
|
23302
|
+
* safe contract navigates nowhere. */
|
|
23303
|
+
const openLinkHref = (href) => {
|
|
23304
|
+
const target = linkOpenTargetOf(href);
|
|
23305
|
+
if (target === null) return;
|
|
23306
|
+
if (target === "new-tab") window.open(href, "_blank", "noopener,noreferrer");
|
|
23307
|
+
else window.location.assign(href);
|
|
23308
|
+
};
|
|
23309
|
+
//#endregion
|
|
23310
|
+
//#region src/editing/link/use-link-popover.tsx
|
|
23311
|
+
/** The editor-level link popover's wiring: the popover's / the context
|
|
23312
|
+
* menu's state, the opener's api (the toolbar's button / the mod+k's
|
|
23313
|
+
* shortcut / the context menu's edit's item open over it — the target's
|
|
23314
|
+
* resolution + the uniform link's prefill), and the root's `contextmenu`'s
|
|
23315
|
+
* handler (the link's anchor's stand — the native menu's suppress over the
|
|
23316
|
+
* link's own menu; the other's targets stand the native's stand). */
|
|
23317
|
+
/** The selection's rect's bottom to the popover's anchor's gap (px). */
|
|
23318
|
+
const ANCHOR_GAP_PX = 8;
|
|
23319
|
+
/** The selection's reseat's window (the tracking's transient mapping's
|
|
23320
|
+
* suppression over the popover / the menu's open's input's focus and the
|
|
23321
|
+
* close's residue — the doc's selection's no-clobber's stand). */
|
|
23322
|
+
const LINK_RESEAT_WINDOW_MS = RESEAT_WINDOW_MS;
|
|
23323
|
+
/** The anchor element's non-empty text descendants (in document order). */
|
|
23324
|
+
function textNodesOf(anchor) {
|
|
23325
|
+
const nodes = [];
|
|
23326
|
+
const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);
|
|
23327
|
+
let current = walker.nextNode();
|
|
23328
|
+
while (current !== null) {
|
|
23329
|
+
if (current instanceof Text && current.nodeValue !== "") nodes.push(current);
|
|
23330
|
+
current = walker.nextNode();
|
|
23331
|
+
}
|
|
23332
|
+
return nodes;
|
|
23333
|
+
}
|
|
23334
|
+
/** The target's resolution over the view's selection (the toolbar's / the
|
|
23335
|
+
* mod+k's stand): the range over the eligible's target, the caret's link's
|
|
23336
|
+
* expansion over a caret (the inside-a-link's stand, the caret's own
|
|
23337
|
+
* insertion's target otherwise). */
|
|
23338
|
+
function targetOfSelection(doc, selection) {
|
|
23339
|
+
if (selection === null || selection.kind !== "text") return null;
|
|
23340
|
+
if (selection.to !== null) return toolbarRangeOf(doc, selection);
|
|
23341
|
+
return linkTargetOfCaret(doc, selection.from);
|
|
23342
|
+
}
|
|
23343
|
+
function useLinkPopover(rootRef) {
|
|
23344
|
+
const { doc, selection: selectionRef, viewMode } = useEditorContext();
|
|
23345
|
+
const [popover, setPopover] = useState(null);
|
|
23346
|
+
const [menu, setMenu] = useState(null);
|
|
23347
|
+
const suppressTransientSelection = () => {
|
|
23348
|
+
selectionRef.reseatUntil.current = Date.now() + LINK_RESEAT_WINDOW_MS;
|
|
23349
|
+
};
|
|
23350
|
+
const closeAll = useCallback(() => {
|
|
23351
|
+
setPopover(null);
|
|
23352
|
+
setMenu(null);
|
|
23353
|
+
suppressTransientSelection();
|
|
23354
|
+
requestAnimationFrame(() => {
|
|
23355
|
+
const active = document.activeElement;
|
|
23356
|
+
if (active === null || active === document.body) rootRef.current?.focus();
|
|
23357
|
+
});
|
|
23358
|
+
}, [rootRef, selectionRef]);
|
|
23359
|
+
const open = useCallback((anchor, opts) => {
|
|
23360
|
+
setMenu(null);
|
|
23361
|
+
const view = selectionRef.view;
|
|
23362
|
+
const target = opts?.target ?? targetOfSelection(doc, view !== null ? view.get() : null);
|
|
23363
|
+
if (target === null) return;
|
|
23364
|
+
const prefilledHref = opts?.prefilledHref ?? uniformLinkHrefOf(doc, target);
|
|
23365
|
+
setPopover({
|
|
23366
|
+
anchor,
|
|
23367
|
+
target,
|
|
23368
|
+
prefilledHref
|
|
23369
|
+
});
|
|
23370
|
+
suppressTransientSelection();
|
|
23371
|
+
}, [doc, selectionRef]);
|
|
23372
|
+
const openFromSelection = useCallback((selection) => {
|
|
23373
|
+
const target = targetOfSelection(doc, selection);
|
|
23374
|
+
if (target === null) return;
|
|
23375
|
+
const root = rootRef.current;
|
|
23376
|
+
let anchor = {
|
|
23377
|
+
x: 8,
|
|
23378
|
+
y: 8
|
|
23379
|
+
};
|
|
23380
|
+
const domSelection = window.getSelection();
|
|
23381
|
+
const range = domSelection !== null && domSelection.rangeCount > 0 ? domSelection.getRangeAt(0) : null;
|
|
23382
|
+
if (root !== null && range !== null && typeof range.getBoundingClientRect === "function") {
|
|
23383
|
+
const rect = range.getBoundingClientRect();
|
|
23384
|
+
const rootRect = root.getBoundingClientRect();
|
|
23385
|
+
anchor = {
|
|
23386
|
+
x: rect.left + rect.width / 2 - rootRect.left,
|
|
23387
|
+
y: rect.bottom - rootRect.top + ANCHOR_GAP_PX
|
|
23388
|
+
};
|
|
23389
|
+
}
|
|
23390
|
+
open(anchor, { target });
|
|
23391
|
+
}, [
|
|
23392
|
+
doc,
|
|
23393
|
+
open,
|
|
23394
|
+
rootRef
|
|
23395
|
+
]);
|
|
23396
|
+
const api = useMemo(() => ({
|
|
23397
|
+
open,
|
|
23398
|
+
openFromSelection
|
|
23399
|
+
}), [open, openFromSelection]);
|
|
23400
|
+
/** The mod+k's interception (the read-only's / the nested editor's /
|
|
23401
|
+
* the form control's stands pass through): the popover's open over the
|
|
23402
|
+
* current view's selection, the no-op's stand over a non-text's view. */
|
|
23403
|
+
const handleKeyDown = useCallback((event) => {
|
|
23404
|
+
if (viewMode) return false;
|
|
23405
|
+
if (!(event.metaKey || event.ctrlKey) || event.shiftKey || event.key.toLowerCase() !== "k") return false;
|
|
23406
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
23407
|
+
if (target !== null && target.closest("input, textarea") !== null) return false;
|
|
23408
|
+
const root = rootRef.current;
|
|
23409
|
+
const formControl = target?.closest("[contenteditable='true']") ?? null;
|
|
23410
|
+
if (root !== null && target !== null && formControl !== null && !root.contains(target)) return false;
|
|
23411
|
+
const nearest = target?.closest("[data-slot=\"docblocks/editor\"]") ?? null;
|
|
23412
|
+
if (root !== null && nearest !== null && nearest !== root) return false;
|
|
23413
|
+
event.preventDefault();
|
|
23414
|
+
const view = selectionRef.view;
|
|
23415
|
+
openFromSelection(view !== null ? view.get() : null);
|
|
23416
|
+
return true;
|
|
23417
|
+
}, [
|
|
23418
|
+
viewMode,
|
|
23419
|
+
rootRef,
|
|
23420
|
+
selectionRef,
|
|
23421
|
+
openFromSelection
|
|
23422
|
+
]);
|
|
23423
|
+
/** The root's contextmenu: over the link's anchor — the link's menu (the
|
|
23424
|
+
* native's suppress); otherwise — the native's stand (the table's cell's
|
|
23425
|
+
* menu / the browser's own stand over the other's targets). */
|
|
23426
|
+
const onContextMenu = useCallback((event) => {
|
|
23427
|
+
const anchorEl = (event.target instanceof Element ? event.target : null)?.closest("a[data-slot='docblocks/link']") ?? null;
|
|
23428
|
+
const root = rootRef.current;
|
|
23429
|
+
if (anchorEl === null || root === null) return;
|
|
23430
|
+
const href = anchorEl.getAttribute("href");
|
|
23431
|
+
const blockId = anchorEl.closest("[data-block-id]")?.dataset.blockId;
|
|
23432
|
+
if (href === null || blockId === void 0) return;
|
|
23433
|
+
const inner = blockInnerOf(rootRef, blockId);
|
|
23434
|
+
const nodes = inner !== null ? textNodesOf(anchorEl) : [];
|
|
23435
|
+
if (inner === null || nodes.length === 0) return;
|
|
23436
|
+
const from = caretOffsetIn(inner, nodes[0], 0);
|
|
23437
|
+
const last = nodes[nodes.length - 1];
|
|
23438
|
+
const to = caretOffsetIn(inner, last, last.nodeValue?.length ?? 0);
|
|
23439
|
+
if (from >= to) return;
|
|
23440
|
+
event.preventDefault();
|
|
23441
|
+
setPopover(null);
|
|
23442
|
+
setMenu({
|
|
23443
|
+
x: event.clientX,
|
|
23444
|
+
y: event.clientY,
|
|
23445
|
+
href,
|
|
23446
|
+
blockId,
|
|
23447
|
+
from,
|
|
23448
|
+
to
|
|
23449
|
+
});
|
|
23450
|
+
}, [rootRef]);
|
|
23451
|
+
const handleOpen = useCallback(() => {
|
|
23452
|
+
if (menu !== null) {
|
|
23453
|
+
openLinkHref(menu.href);
|
|
23454
|
+
closeAll();
|
|
23455
|
+
}
|
|
23456
|
+
}, [closeAll, menu]);
|
|
23457
|
+
const handleCopy = useCallback(() => {
|
|
23458
|
+
if (menu === null) return;
|
|
23459
|
+
try {
|
|
23460
|
+
navigator.clipboard.writeText(menu.href);
|
|
23461
|
+
} catch {}
|
|
23462
|
+
closeAll();
|
|
23463
|
+
}, [closeAll, menu]);
|
|
23464
|
+
const handleEdit = useCallback(() => {
|
|
23465
|
+
if (menu === null) return;
|
|
23466
|
+
const root = rootRef.current;
|
|
23467
|
+
if (root === null) return;
|
|
23468
|
+
const rootRect = root.getBoundingClientRect();
|
|
23469
|
+
open({
|
|
23470
|
+
x: menu.x - rootRect.left,
|
|
23471
|
+
y: menu.y - rootRect.top
|
|
23472
|
+
}, {
|
|
23473
|
+
target: {
|
|
23474
|
+
kind: "single",
|
|
23475
|
+
blockId: menu.blockId,
|
|
23476
|
+
from: menu.from,
|
|
23477
|
+
to: menu.to
|
|
23478
|
+
},
|
|
23479
|
+
prefilledHref: menu.href
|
|
23480
|
+
});
|
|
23481
|
+
}, [
|
|
23482
|
+
menu,
|
|
23483
|
+
open,
|
|
23484
|
+
rootRef
|
|
23485
|
+
]);
|
|
23486
|
+
const handleRemove = useCallback(() => {
|
|
23487
|
+
if (menu === null) return;
|
|
23488
|
+
removeLink(doc, {
|
|
23489
|
+
kind: "single",
|
|
23490
|
+
blockId: menu.blockId,
|
|
23491
|
+
from: menu.from,
|
|
23492
|
+
to: menu.to
|
|
23493
|
+
});
|
|
23494
|
+
closeAll();
|
|
23495
|
+
}, [
|
|
23496
|
+
closeAll,
|
|
23497
|
+
doc,
|
|
23498
|
+
menu
|
|
23499
|
+
]);
|
|
23500
|
+
return {
|
|
23501
|
+
api,
|
|
23502
|
+
handleKeyDown,
|
|
23503
|
+
onContextMenu,
|
|
23504
|
+
node: /* @__PURE__ */ jsxs(Fragment$1, { children: [menu !== null && /* @__PURE__ */ jsx(LinkMenu, {
|
|
23505
|
+
menu,
|
|
23506
|
+
viewMode,
|
|
23507
|
+
onOpen: handleOpen,
|
|
23508
|
+
onCopy: handleCopy,
|
|
23509
|
+
onEdit: handleEdit,
|
|
23510
|
+
onRemove: handleRemove,
|
|
23511
|
+
onClose: closeAll
|
|
23512
|
+
}), popover !== null && /* @__PURE__ */ jsx(LinkPopover, {
|
|
23513
|
+
rootRef,
|
|
23514
|
+
state: popover,
|
|
23515
|
+
onClose: closeAll
|
|
23516
|
+
})] })
|
|
23517
|
+
};
|
|
23518
|
+
}
|
|
23519
|
+
//#endregion
|
|
22739
23520
|
//#region src/editing/format-toolbar/FormatListMenu.tsx
|
|
22740
23521
|
/** The format toolbar's LIST-TYPE menu (single-block paragraph / heading /
|
|
22741
23522
|
* list-item targets only): a non-modal dropdown — a radio group that turns
|
|
@@ -22947,6 +23728,7 @@ function createFormatToolbarActions({ doc, range, single, target, rootRef, onPal
|
|
|
22947
23728
|
const alignActive = (align) => range.kind === "cross" ? uniformAlignOfRange(doc, range.cross) === align : alignOfBlock(single) === align;
|
|
22948
23729
|
const paletteActive = (value) => range.kind === "cross" ? uniformMarkValueRange(doc, range.cross, target) === value : uniformMarkValue(single, range.from, range.to, target) === value;
|
|
22949
23730
|
const currentMarkValue = range.kind === "cross" ? uniformMarkValueRange(doc, range.cross, target) : uniformMarkValue(single, range.from, range.to, target);
|
|
23731
|
+
const linkUniformHref = uniformLinkHrefOf(doc, range);
|
|
22950
23732
|
return {
|
|
22951
23733
|
onToggleMark,
|
|
22952
23734
|
onAlign,
|
|
@@ -22956,7 +23738,8 @@ function createFormatToolbarActions({ doc, range, single, target, rootRef, onPal
|
|
|
22956
23738
|
alignActive,
|
|
22957
23739
|
paletteActive,
|
|
22958
23740
|
currentMarkValue,
|
|
22959
|
-
chipValue: currentMarkValue !== null && currentMarkValue !== "mixed" ? currentMarkValue : null
|
|
23741
|
+
chipValue: currentMarkValue !== null && currentMarkValue !== "mixed" ? currentMarkValue : null,
|
|
23742
|
+
linkUniformHref
|
|
22960
23743
|
};
|
|
22961
23744
|
}
|
|
22962
23745
|
//#endregion
|
|
@@ -22988,27 +23771,12 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
22988
23771
|
const { t } = useTranslation("editor");
|
|
22989
23772
|
const { doc } = useEditorContext();
|
|
22990
23773
|
const selection = useSelection();
|
|
23774
|
+
const linkApi = useLinkPopoverApi();
|
|
22991
23775
|
const barRef = useRef(null);
|
|
22992
23776
|
const [paletteOpen, setPaletteOpen] = useState(false);
|
|
22993
23777
|
const [listMenuOpen, setListMenuOpen] = useState(false);
|
|
22994
23778
|
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]);
|
|
23779
|
+
const range = useMemo(() => toolbarRangeOf(doc, selection), [doc, selection]);
|
|
23012
23780
|
const block = useBlock(range !== null && range.kind === "single" ? range.blockId : "");
|
|
23013
23781
|
const selectionKey = selection.kind === "text" ? `${selection.from.blockId}:${selection.from.offset}:${selection.to?.blockId ?? ""}:${selection.to?.offset ?? "caret"}` : selection.kind;
|
|
23014
23782
|
useEffect(() => {
|
|
@@ -23048,6 +23816,17 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
23048
23816
|
onListMenuClose: () => setListMenuOpen(false)
|
|
23049
23817
|
});
|
|
23050
23818
|
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" : "";
|
|
23819
|
+
const onLinkClick = () => {
|
|
23820
|
+
const bar = barRef.current;
|
|
23821
|
+
const root = rootRef.current;
|
|
23822
|
+
if (bar === null || root === null || range === null || linkApi === null) return;
|
|
23823
|
+
const barRect = bar.getBoundingClientRect();
|
|
23824
|
+
const rootRect = root.getBoundingClientRect();
|
|
23825
|
+
linkApi.open({
|
|
23826
|
+
x: barRect.left - rootRect.left,
|
|
23827
|
+
y: barRect.bottom - rootRect.top + 4
|
|
23828
|
+
}, { target: range });
|
|
23829
|
+
};
|
|
23051
23830
|
return /* @__PURE__ */ jsxs("div", {
|
|
23052
23831
|
"data-slot": "docblocks/format-toolbar",
|
|
23053
23832
|
ref: barRef,
|
|
@@ -23067,6 +23846,17 @@ function FormatToolbar({ rootRef, palette }) {
|
|
|
23067
23846
|
onClick: () => actions.onToggleMark(mark),
|
|
23068
23847
|
children: /* @__PURE__ */ jsx(Icon, { className: "size-4" })
|
|
23069
23848
|
}, mark)),
|
|
23849
|
+
/* @__PURE__ */ jsx(Button, {
|
|
23850
|
+
variant: "ghost",
|
|
23851
|
+
size: "icon",
|
|
23852
|
+
"data-link-btn": true,
|
|
23853
|
+
title: t("formatToolbar.marks.link"),
|
|
23854
|
+
"aria-label": t("formatToolbar.marks.link"),
|
|
23855
|
+
"aria-pressed": actions.linkUniformHref !== null,
|
|
23856
|
+
className: cn(actions.linkUniformHref !== null && "bg-accent text-accent-foreground"),
|
|
23857
|
+
onClick: onLinkClick,
|
|
23858
|
+
children: /* @__PURE__ */ jsx(Link, { className: "size-4" })
|
|
23859
|
+
}),
|
|
23070
23860
|
/* @__PURE__ */ jsx(Separator, {
|
|
23071
23861
|
orientation: "vertical",
|
|
23072
23862
|
className: "h-5"
|
|
@@ -28884,6 +29674,7 @@ function useTableCellMenu({ doc, viewMode, frameRef }) {
|
|
|
28884
29674
|
*/
|
|
28885
29675
|
const onTableContextMenu = (event) => {
|
|
28886
29676
|
if (viewMode) return;
|
|
29677
|
+
if (event.target.closest?.("a[data-slot='docblocks/link']") !== null) return;
|
|
28887
29678
|
const cell = event.target.closest("td[data-block-id]");
|
|
28888
29679
|
if (cell === null) return;
|
|
28889
29680
|
const cellId = cell.dataset.blockId ?? "";
|
|
@@ -35110,8 +35901,6 @@ function usePageMousedownGuard(rootRef, readOnly) {
|
|
|
35110
35901
|
if (active instanceof HTMLElement && (active.matches("input, textarea") || activeIsland !== null && activeEditable !== null && activeIsland.contains(activeEditable))) return;
|
|
35111
35902
|
if (island === null && editable instanceof HTMLElement && isPageSurfaceInner(editable)) return;
|
|
35112
35903
|
event.preventDefault();
|
|
35113
|
-
const a = document.activeElement;
|
|
35114
|
-
if (a === null || !root.contains(a)) root.focus();
|
|
35115
35904
|
};
|
|
35116
35905
|
window.addEventListener("mousedown", guard, true);
|
|
35117
35906
|
return () => window.removeEventListener("mousedown", guard, true);
|
|
@@ -35821,46 +36610,6 @@ function handleDocumentEdge(event, deps) {
|
|
|
35821
36610
|
return true;
|
|
35822
36611
|
}
|
|
35823
36612
|
//#endregion
|
|
35824
|
-
//#region src/editing/editor-root/history-reseat.ts
|
|
35825
|
-
/**
|
|
35826
|
-
* The history reseat's window over the undo/redo (the remount's transient
|
|
35827
|
-
* selection loss's stand): the window's end stands the mapping's none's
|
|
35828
|
-
* suppression. The engine's remount commit + the selectionchange's settle
|
|
35829
|
-
* stands the sub-second over the load, so the window stands a margin over
|
|
35830
|
-
* it (the staleness is bounded by the window's end; the explicit paths —
|
|
35831
|
-
* the Escape, the gutter's reset — stand the real clear).
|
|
35832
|
-
*/
|
|
35833
|
-
const RESEAT_WINDOW_MS = 2e3;
|
|
35834
|
-
/**
|
|
35835
|
-
* The undo/redo's reseat over the engine's event ordering: the fast path
|
|
35836
|
-
* re-reads the view over the synchronous read (the restoration's stand
|
|
35837
|
-
* lands inside the undo/redo call), and the window's stand re-runs the
|
|
35838
|
-
* reseat when the restoration's emission lands over the transaction's
|
|
35839
|
-
* settle (the engine's ordering stands the popped's event after the
|
|
35840
|
-
* handler's return — the reseat then reads the restored state). The
|
|
35841
|
-
* reseat's own stands (the user's live selection's stop, the seated's
|
|
35842
|
-
* short-circuit) keep the re-runs from clobbering.
|
|
35843
|
-
*/
|
|
35844
|
-
function scheduleHistoryReseat(doc, view, root) {
|
|
35845
|
-
if (view === null) return;
|
|
35846
|
-
const scope = root ?? document;
|
|
35847
|
-
const run = () => {
|
|
35848
|
-
reseatHistorySelection(doc, view, scope);
|
|
35849
|
-
};
|
|
35850
|
-
run();
|
|
35851
|
-
let alive = true;
|
|
35852
|
-
const unsubscribe = view.onChange((selection) => {
|
|
35853
|
-
if (!alive) return;
|
|
35854
|
-
if (selection.kind !== "text") return;
|
|
35855
|
-
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => alive && run());
|
|
35856
|
-
else window.setTimeout(() => alive && run(), 0);
|
|
35857
|
-
});
|
|
35858
|
-
window.setTimeout(() => {
|
|
35859
|
-
alive = false;
|
|
35860
|
-
unsubscribe();
|
|
35861
|
-
}, RESEAT_WINDOW_MS);
|
|
35862
|
-
}
|
|
35863
|
-
//#endregion
|
|
35864
36613
|
//#region src/editing/editor-root/selection-tracking.ts
|
|
35865
36614
|
/** The editor root's `selectionchange` mapping: the DOM selection → the
|
|
35866
36615
|
* core Selection view (the editing stand's tracking). */
|
|
@@ -35883,7 +36632,10 @@ function useSelectionTracking(doc, rootRef, selectionRef, viewMode) {
|
|
|
35883
36632
|
return;
|
|
35884
36633
|
}
|
|
35885
36634
|
const next = mapDomSelection(root, window.getSelection(), doc);
|
|
35886
|
-
|
|
36635
|
+
const current = view.get();
|
|
36636
|
+
const linkAnchor = window.getSelection()?.anchorNode ?? null;
|
|
36637
|
+
if ((linkAnchor instanceof Element ? linkAnchor : linkAnchor?.parentElement ?? null)?.closest("[data-slot='docblocks/link-popover'], [data-slot='docblocks/link-menu']") !== null) return;
|
|
36638
|
+
if (current.kind === "blocks" && !domSelectionInEditable(window.getSelection())) return;
|
|
35887
36639
|
const anchorNode = window.getSelection()?.anchorNode ?? null;
|
|
35888
36640
|
if ((anchorNode instanceof Element ? anchorNode : anchorNode?.parentElement ?? null)?.closest("[data-slot=\"docblocks/page-card\"]") !== null) return;
|
|
35889
36641
|
if (next.kind !== "text" && Date.now() < selectionRef.reseatUntil.current) return;
|
|
@@ -35997,7 +36749,7 @@ function useLinkOpen() {
|
|
|
35997
36749
|
const target = event.target;
|
|
35998
36750
|
const anchor = target instanceof Element ? target.closest("a[href]") : null;
|
|
35999
36751
|
const href = anchor !== null ? anchor.getAttribute("href") : null;
|
|
36000
|
-
pressRef.current = anchor !== null && href !== null &&
|
|
36752
|
+
pressRef.current = anchor !== null && href !== null && linkHrefKindOf(href) !== null ? {
|
|
36001
36753
|
href,
|
|
36002
36754
|
x: event.clientX,
|
|
36003
36755
|
y: event.clientY
|
|
@@ -36011,7 +36763,7 @@ function useLinkOpen() {
|
|
|
36011
36763
|
const dy = event.clientY - press.y;
|
|
36012
36764
|
if (dx * dx + dy * dy > 16) return;
|
|
36013
36765
|
event.preventDefault();
|
|
36014
|
-
|
|
36766
|
+
openLinkHref(press.href);
|
|
36015
36767
|
}, [])
|
|
36016
36768
|
};
|
|
36017
36769
|
}
|
|
@@ -36999,6 +37751,11 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
36999
37751
|
rootChildren
|
|
37000
37752
|
]);
|
|
37001
37753
|
const { rootRef, onKeyDown, onCopy, onPointerDown: onCrossDragPointerDown } = useEditing();
|
|
37754
|
+
const linkPopover = useLinkPopover(rootRef);
|
|
37755
|
+
const handleKeyDown = (event) => {
|
|
37756
|
+
if (linkPopover.handleKeyDown(event)) return;
|
|
37757
|
+
onKeyDown(event);
|
|
37758
|
+
};
|
|
37002
37759
|
const linkOpen = useLinkOpen();
|
|
37003
37760
|
const slash = useSlashMenu(rootRef);
|
|
37004
37761
|
const mention = useMentionMenu(rootRef);
|
|
@@ -37064,8 +37821,9 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
37064
37821
|
"data-mention-interactive": mentionInteractions !== null ? "true" : void 0,
|
|
37065
37822
|
tabIndex: -1,
|
|
37066
37823
|
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)"),
|
|
37067
|
-
onKeyDown,
|
|
37824
|
+
onKeyDown: handleKeyDown,
|
|
37068
37825
|
onCopy,
|
|
37826
|
+
onContextMenu: linkPopover.onContextMenu,
|
|
37069
37827
|
onMouseDown: linkOpen.onMouseDown,
|
|
37070
37828
|
onClick: linkOpen.onClick,
|
|
37071
37829
|
onPointerDown: onCrossDragPointerDown,
|
|
@@ -37112,7 +37870,11 @@ function Editor({ view = "page", formatPalette = FORMAT_PALETTE }) {
|
|
|
37112
37870
|
slash.menuNode,
|
|
37113
37871
|
mention.menuNode,
|
|
37114
37872
|
mentionCard,
|
|
37115
|
-
|
|
37873
|
+
/* @__PURE__ */ jsx(LinkPopoverContext.Provider, {
|
|
37874
|
+
value: linkPopover.api,
|
|
37875
|
+
children: viewMode ? null : format.toolbarNode
|
|
37876
|
+
}),
|
|
37877
|
+
linkPopover.node
|
|
37116
37878
|
]
|
|
37117
37879
|
})
|
|
37118
37880
|
})]
|