@segnavia/markdown-editor 0.0.0-stage → 0.42.0-setup.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 modstudio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ //#region src/editor-css.ts
2
+ const EDITOR_CSS = `
3
+ :where(:host(segnavia-markdown-editor:not([unstyled]))){display:block}
4
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="editor"]){display:grid;grid-template-rows:auto minmax(0,1fr);min-block-size:0}
5
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="title"]){box-sizing:border-box;inline-size:100%}
6
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="source"]){min-inline-size:0;min-block-size:0}
7
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="source"] .cm-editor){block-size:100%}
8
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"]){min-inline-size:0;min-block-size:0;overflow:auto}
9
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] .ProseMirror){min-block-size:100%;outline:0}
10
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] table){border-collapse:collapse;inline-size:max-content;min-inline-size:100%}
11
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] th),:where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] td){min-inline-size:1em}
12
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] [data-type="taskList"]){list-style:none;padding-inline-start:0}
13
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="rich"] [data-type="taskItem"]){display:grid;grid-template-columns:auto minmax(0,1fr);gap:var(--segnavia-markdown-task-gap,.5em)}
14
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="locked-block"]){display:block;position:relative;min-block-size:1em}
15
+ :where(:host(segnavia-markdown-editor:not([unstyled])) [part~="locked-block"] segnavia-page){display:block}
16
+ `;
17
+ //#endregion
18
+ export { EDITOR_CSS };
@@ -0,0 +1,8 @@
1
+ import { EDITOR_CSS } from "./editor-css.js";
2
+ import { ensureStyle } from "@segnavia/frame";
3
+ //#region src/editor-style.ts
4
+ function ensureEditorStyle(element) {
5
+ ensureStyle(element, "markdown-editor", EDITOR_CSS);
6
+ }
7
+ //#endregion
8
+ export { ensureEditorStyle };
@@ -0,0 +1,32 @@
1
+ import { registerMarkdownEditorFonts } from "./markdown-editor-fonts.js";
2
+ //#region src/markdown-editor-font-registration.ts
3
+ var MarkdownEditorFontRegistration = class {
4
+ element;
5
+ loadController;
6
+ remove;
7
+ constructor(element) {
8
+ this.element = element;
9
+ }
10
+ clear() {
11
+ this.loadController?.abort();
12
+ this.loadController = void 0;
13
+ this.remove?.();
14
+ this.remove = void 0;
15
+ }
16
+ load(references, fonts) {
17
+ this.clear();
18
+ if (!this.element.isConnected || references.length === 0) return;
19
+ const controller = new AbortController();
20
+ this.loadController = controller;
21
+ registerMarkdownEditorFonts(this.element, references, fonts, controller.signal).then((remove) => {
22
+ if (!controller.signal.aborted) this.remove = remove;
23
+ }).catch((error) => {
24
+ if (!controller.signal.aborted) this.element.dispatchEvent(new ErrorEvent("error", {
25
+ error,
26
+ message: error instanceof Error ? error.message : String(error)
27
+ }));
28
+ });
29
+ }
30
+ };
31
+ //#endregion
32
+ export { MarkdownEditorFontRegistration };
@@ -0,0 +1,56 @@
1
+ import { FontRefusedError, appNames, refuseMissingFontSource, requireAppFontName, uniqueFontReferences } from "@segnavia/frame/fonts";
2
+ //#region src/markdown-editor-fonts.ts
3
+ function nativeFace(face) {
4
+ const descriptors = {};
5
+ if (face.weight !== void 0) descriptors.weight = String(face.weight);
6
+ if (face.style !== void 0) descriptors.style = face.style;
7
+ if (face.unicodeRange !== void 0) descriptors.unicodeRange = face.unicodeRange;
8
+ return new FontFace(face.family, new Uint8Array(face.data).buffer, descriptors);
9
+ }
10
+ async function registerMarkdownEditorFonts(element, references, fonts, signal) {
11
+ const unique = uniqueFontReferences(references);
12
+ if (!fonts) {
13
+ refuseMissingFontSource(unique, "markdown editor");
14
+ return () => void 0;
15
+ }
16
+ const app = /* @__PURE__ */ new Map();
17
+ const loaded = [];
18
+ const properties = [];
19
+ let cleaned = false;
20
+ const cleanup = () => {
21
+ if (cleaned) return;
22
+ cleaned = true;
23
+ for (const face of loaded) document.fonts.delete(face);
24
+ for (const property of properties) element.style.removeProperty(property);
25
+ };
26
+ signal.addEventListener("abort", cleanup, { once: true });
27
+ try {
28
+ for (const name of appNames(unique)) {
29
+ requireAppFontName(name, "markdown editor");
30
+ const resolved = await fonts.source.font(name, { signal });
31
+ signal.throwIfAborted();
32
+ if (!resolved) throw new FontRefusedError(`markdown editor could not establish app font ${JSON.stringify(name)}; make that font available from the source to clear this refusal.`);
33
+ app.set(name, resolved);
34
+ const property = `--segnavia-font-${name}`;
35
+ element.style.setProperty(property, `${JSON.stringify(resolved.family)},${resolved.generic ?? "sans-serif"}`);
36
+ properties.push(property);
37
+ }
38
+ for (const face of [...app.values()].flatMap((font) => font.faces).concat(fonts.local ?? [])) {
39
+ const native = nativeFace(face);
40
+ await native.load();
41
+ signal.throwIfAborted();
42
+ document.fonts.add(native);
43
+ loaded.push(native);
44
+ }
45
+ signal.throwIfAborted();
46
+ } catch (error) {
47
+ cleanup();
48
+ throw error;
49
+ }
50
+ return () => {
51
+ signal.removeEventListener("abort", cleanup);
52
+ cleanup();
53
+ };
54
+ }
55
+ //#endregion
56
+ export { registerMarkdownEditorFonts };
@@ -0,0 +1,9 @@
1
+ import { safeMarkdownUrl } from "@segnavia/html";
2
+ //#region src/markdown-editor-link.ts
3
+ function validateMarkdownLink(href, linkOrigins) {
4
+ if (safeMarkdownUrl("href", href, linkOrigins)) return;
5
+ const origins = linkOrigins === void 0 ? "" : ", or an origin in linkOrigins";
6
+ throw new Error(`Refused link href ${JSON.stringify(href)}; use an allowed scheme${origins} to clear this refusal.`);
7
+ }
8
+ //#endregion
9
+ export { validateMarkdownLink };
@@ -0,0 +1,3 @@
1
+ //#region src/markdown-editor-model.d.ts
2
+ export type MarkdownTitleSource = 'separate' | 'leading-heading';
3
+ //#endregion
@@ -0,0 +1,76 @@
1
+ import { joinLeadingHeading, normalizeMarkdownTitle, retainedLeadingHeading, splitLeadingHeading } from "./markdown-title.js";
2
+ //#region src/markdown-editor-model.ts
3
+ function markdownEditorModel() {
4
+ return {
5
+ markdown: "",
6
+ title: null,
7
+ titleSource: "separate",
8
+ editable: "",
9
+ retained: ""
10
+ };
11
+ }
12
+ function setModelMarkdown(model, markdown) {
13
+ if (markdown === model.markdown) return model;
14
+ if (model.titleSource === "separate") return {
15
+ ...model,
16
+ markdown,
17
+ editable: markdown
18
+ };
19
+ const split = splitLeadingHeading(markdown);
20
+ return {
21
+ ...model,
22
+ markdown,
23
+ title: split.title ?? "",
24
+ editable: split.editable,
25
+ retained: split.retained
26
+ };
27
+ }
28
+ function setModelTitleSource(model, titleSource) {
29
+ if (titleSource === model.titleSource) return model;
30
+ if (titleSource === "separate") return {
31
+ ...model,
32
+ titleSource,
33
+ editable: model.markdown,
34
+ retained: ""
35
+ };
36
+ const split = splitLeadingHeading(model.markdown);
37
+ return {
38
+ ...model,
39
+ titleSource,
40
+ title: split.title ?? "",
41
+ editable: split.editable,
42
+ retained: split.retained
43
+ };
44
+ }
45
+ function setModelTitle(model, value) {
46
+ if (model.titleSource === "leading-heading" && value == null) return model;
47
+ const title = value == null ? null : normalizeMarkdownTitle(String(value));
48
+ if (title === model.title) return model;
49
+ if (model.titleSource === "separate") return {
50
+ ...model,
51
+ title
52
+ };
53
+ const retained = retainedLeadingHeading(title, {
54
+ title: model.title || null,
55
+ editable: model.editable,
56
+ retained: model.retained
57
+ });
58
+ const markdown = joinLeadingHeading(model.editable, retained);
59
+ return {
60
+ ...model,
61
+ markdown,
62
+ title,
63
+ retained
64
+ };
65
+ }
66
+ function setModelEditable(model, editable) {
67
+ const markdown = model.titleSource === "leading-heading" ? joinLeadingHeading(editable, model.retained) : editable;
68
+ if (markdown === model.markdown && editable === model.editable) return model;
69
+ return {
70
+ ...model,
71
+ markdown,
72
+ editable
73
+ };
74
+ }
75
+ //#endregion
76
+ export { markdownEditorModel, setModelEditable, setModelMarkdown, setModelTitle, setModelTitleSource };
@@ -0,0 +1,38 @@
1
+ import { uniqueFontReferences } from "@segnavia/frame/fonts";
2
+ //#region src/markdown-editor-options.ts
3
+ function equalValue(left, right) {
4
+ if (Object.is(left, right)) return true;
5
+ if (ArrayBuffer.isView(left) && ArrayBuffer.isView(right)) {
6
+ const a = new Uint8Array(left.buffer, left.byteOffset, left.byteLength);
7
+ const b = new Uint8Array(right.buffer, right.byteOffset, right.byteLength);
8
+ return a.length === b.length && a.every((value, index) => value === b[index]);
9
+ }
10
+ if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((value, index) => equalValue(value, right[index]));
11
+ if (!left || !right || typeof left !== "object" || typeof right !== "object") return false;
12
+ const leftEntries = Object.entries(left);
13
+ const rightRecord = right;
14
+ return leftEntries.length === Object.keys(rightRecord).length && leftEntries.every(([key, value]) => Object.hasOwn(rightRecord, key) && equalValue(value, rightRecord[key]));
15
+ }
16
+ function normalizeLockedFences(value) {
17
+ return Object.fromEntries(Object.entries(value ?? {}).map(([name, label]) => [name.toLowerCase(), String(label)]));
18
+ }
19
+ function equalLockedFences(left, right) {
20
+ return equalValue(left, right);
21
+ }
22
+ function equalAlertLabels(left, right) {
23
+ return equalValue(left, right);
24
+ }
25
+ function normalizeAlertLabels(value) {
26
+ return value && Object.fromEntries(Object.entries(value).map(([kind, label]) => [kind, String(label)]));
27
+ }
28
+ function normalizeFontReferences(value) {
29
+ return uniqueFontReferences([...value ?? []]);
30
+ }
31
+ function equalFontReferences(left, right) {
32
+ return equalValue(left, right);
33
+ }
34
+ function equalFonts(left, right) {
35
+ return equalValue(left, right);
36
+ }
37
+ //#endregion
38
+ export { equalAlertLabels, equalFontReferences, equalFonts, equalLockedFences, normalizeAlertLabels, normalizeFontReferences, normalizeLockedFences };
@@ -0,0 +1,83 @@
1
+ //#region src/markdown-editor-policy.d.ts
2
+ export declare const MARKDOWN_EDITOR_MODES: readonly ["rich", "source"];
3
+ export type MarkdownEditorMode = (typeof MARKDOWN_EDITOR_MODES)[number];
4
+ export type MarkdownEditorCommand = 'bold' | 'italic' | 'code' | 'strike' | 'paragraph' | 'heading' | 'bullet-list' | 'ordered-list' | 'task-list' | 'quote' | 'code-block' | 'link' | 'unlink' | 'insert-table' | 'add-row' | 'delete-row' | 'add-column' | 'delete-column' | 'delete-table' | 'undo' | 'redo' | 'toggle-mode' | 'edit-in-source';
5
+ export type MarkdownEditorCommandArgs = {
6
+ heading: {
7
+ level: 1 | 2 | 3 | 4 | 5 | 6;
8
+ };
9
+ link: {
10
+ href: string;
11
+ };
12
+ };
13
+ export declare const MARKDOWN_EDITOR_COMMANDS: readonly [{
14
+ readonly name: "bold";
15
+ readonly key: "Mod-b";
16
+ }, {
17
+ readonly name: "italic";
18
+ readonly key: "Mod-i";
19
+ }, {
20
+ readonly name: "code";
21
+ readonly key: "Mod-e";
22
+ }, {
23
+ readonly name: "strike";
24
+ readonly key: "Mod-Shift-s";
25
+ }, {
26
+ readonly name: "paragraph";
27
+ readonly key: "Mod-Alt-0";
28
+ }, {
29
+ readonly name: "heading";
30
+ readonly key: "Mod-Alt-1..6";
31
+ }, {
32
+ readonly name: "bullet-list";
33
+ readonly key: "Mod-Shift-8";
34
+ }, {
35
+ readonly name: "ordered-list";
36
+ readonly key: "Mod-Shift-7";
37
+ }, {
38
+ readonly name: "task-list";
39
+ readonly key: "Mod-Shift-9";
40
+ }, {
41
+ readonly name: "quote";
42
+ readonly key: "Mod-Shift-b";
43
+ }, {
44
+ readonly name: "code-block";
45
+ readonly key: "Mod-Alt-c";
46
+ }, {
47
+ readonly name: "link";
48
+ readonly key: "Mod-k";
49
+ }, {
50
+ readonly name: "unlink";
51
+ readonly key: "Mod-Shift-k";
52
+ }, {
53
+ readonly name: "insert-table";
54
+ readonly key: "Mod-Alt-Shift-t";
55
+ }, {
56
+ readonly name: "add-row";
57
+ readonly key: "Mod-Alt-Shift-r";
58
+ }, {
59
+ readonly name: "delete-row";
60
+ readonly key: "Mod-Alt-Shift-Backspace";
61
+ }, {
62
+ readonly name: "add-column";
63
+ readonly key: "Mod-Alt-Shift-c";
64
+ }, {
65
+ readonly name: "delete-column";
66
+ readonly key: "Mod-Alt-Shift-Delete";
67
+ }, {
68
+ readonly name: "delete-table";
69
+ readonly key: "Mod-Alt-Shift-x";
70
+ }, {
71
+ readonly name: "undo";
72
+ readonly key: "Mod-z";
73
+ }, {
74
+ readonly name: "redo";
75
+ readonly key: "Mod-Shift-z";
76
+ }, {
77
+ readonly name: "toggle-mode";
78
+ readonly key: "Mod-Alt-m";
79
+ }, {
80
+ readonly name: "edit-in-source";
81
+ readonly key: "Enter";
82
+ }];
83
+ //#endregion
@@ -0,0 +1,117 @@
1
+ //#region src/markdown-editor-policy.ts
2
+ const MARKDOWN_EDITOR_MODES = ["rich", "source"];
3
+ const MARKDOWN_EDITOR_COMMANDS = [
4
+ {
5
+ name: "bold",
6
+ key: "Mod-b"
7
+ },
8
+ {
9
+ name: "italic",
10
+ key: "Mod-i"
11
+ },
12
+ {
13
+ name: "code",
14
+ key: "Mod-e"
15
+ },
16
+ {
17
+ name: "strike",
18
+ key: "Mod-Shift-s"
19
+ },
20
+ {
21
+ name: "paragraph",
22
+ key: "Mod-Alt-0"
23
+ },
24
+ {
25
+ name: "heading",
26
+ key: "Mod-Alt-1..6"
27
+ },
28
+ {
29
+ name: "bullet-list",
30
+ key: "Mod-Shift-8"
31
+ },
32
+ {
33
+ name: "ordered-list",
34
+ key: "Mod-Shift-7"
35
+ },
36
+ {
37
+ name: "task-list",
38
+ key: "Mod-Shift-9"
39
+ },
40
+ {
41
+ name: "quote",
42
+ key: "Mod-Shift-b"
43
+ },
44
+ {
45
+ name: "code-block",
46
+ key: "Mod-Alt-c"
47
+ },
48
+ {
49
+ name: "link",
50
+ key: "Mod-k"
51
+ },
52
+ {
53
+ name: "unlink",
54
+ key: "Mod-Shift-k"
55
+ },
56
+ {
57
+ name: "insert-table",
58
+ key: "Mod-Alt-Shift-t"
59
+ },
60
+ {
61
+ name: "add-row",
62
+ key: "Mod-Alt-Shift-r"
63
+ },
64
+ {
65
+ name: "delete-row",
66
+ key: "Mod-Alt-Shift-Backspace"
67
+ },
68
+ {
69
+ name: "add-column",
70
+ key: "Mod-Alt-Shift-c"
71
+ },
72
+ {
73
+ name: "delete-column",
74
+ key: "Mod-Alt-Shift-Delete"
75
+ },
76
+ {
77
+ name: "delete-table",
78
+ key: "Mod-Alt-Shift-x"
79
+ },
80
+ {
81
+ name: "undo",
82
+ key: "Mod-z"
83
+ },
84
+ {
85
+ name: "redo",
86
+ key: "Mod-Shift-z"
87
+ },
88
+ {
89
+ name: "toggle-mode",
90
+ key: "Mod-Alt-m"
91
+ },
92
+ {
93
+ name: "edit-in-source",
94
+ key: "Enter"
95
+ }
96
+ ];
97
+ const MARKDOWN_EDITOR_KEYS = [{
98
+ key: "Mod-s",
99
+ action: "save"
100
+ }, {
101
+ key: "Mod-Alt-m",
102
+ action: "toggle-mode"
103
+ }];
104
+ function availableMarkdownMode(requested) {
105
+ return MARKDOWN_EDITOR_MODES.includes(requested) ? requested : MARKDOWN_EDITOR_MODES[0];
106
+ }
107
+ function markdownEditorKeyAction(event) {
108
+ if (!(event.metaKey || event.ctrlKey) || event.shiftKey) return null;
109
+ const letter = event.code.startsWith("Key") ? event.code.slice(3).toLowerCase() : event.key.toLowerCase();
110
+ const key = `mod-${event.altKey ? "alt-" : ""}${letter}`;
111
+ return MARKDOWN_EDITOR_KEYS.find((binding) => binding.key.toLowerCase() === key)?.action ?? null;
112
+ }
113
+ function richModeCommandRefusal(command) {
114
+ return /* @__PURE__ */ new Error(`Cannot run ${JSON.stringify(command)} because that command needs rich mode.`);
115
+ }
116
+ //#endregion
117
+ export { MARKDOWN_EDITOR_COMMANDS, MARKDOWN_EDITOR_KEYS, MARKDOWN_EDITOR_MODES, availableMarkdownMode, markdownEditorKeyAction, richModeCommandRefusal };
@@ -0,0 +1,28 @@
1
+ import { MarkdownTitleSource } from "./markdown-editor-model.js";
2
+ import { MarkdownEditorCommand, MarkdownEditorCommandArgs, MarkdownEditorMode } from "./markdown-editor-policy.js";
3
+ import { AlertLabels } from "@segnavia/html";
4
+ import { FontsOptions } from "@segnavia/frame/fonts";
5
+ import { FontReference } from "@segnavia/format";
6
+ //#region src/markdown-editor-types.d.ts
7
+ export type MarkdownEditorStateDetail = Readonly<{
8
+ marks: string[];
9
+ nodes: string[];
10
+ canUndo: boolean;
11
+ canRedo: boolean;
12
+ }>;
13
+ export interface SegnaviaMarkdownEditor extends Omit<HTMLElement, 'title'> {
14
+ markdown: string;
15
+ mode: MarkdownEditorMode;
16
+ readonly modes: readonly ['rich', 'source'];
17
+ title: string | null;
18
+ titleSource: MarkdownTitleSource;
19
+ lockedFences: Record<string, string>;
20
+ css: string;
21
+ fonts: FontsOptions | undefined;
22
+ fontReferences: FontReference[];
23
+ linkOrigins: Iterable<string> | undefined;
24
+ alertLabels: Partial<AlertLabels> | undefined;
25
+ unstyled: boolean;
26
+ run<C extends MarkdownEditorCommand>(command: C, args?: C extends keyof MarkdownEditorCommandArgs ? MarkdownEditorCommandArgs[C] : never): boolean;
27
+ }
28
+ //#endregion
@@ -0,0 +1,12 @@
1
+ import { MarkdownTitleSource } from "./markdown-editor-model.js";
2
+ import { MARKDOWN_EDITOR_COMMANDS, MARKDOWN_EDITOR_MODES, MarkdownEditorCommand, MarkdownEditorMode } from "./markdown-editor-policy.js";
3
+ import { MarkdownEditorStateDetail, SegnaviaMarkdownEditor as SegnaviaMarkdownEditor$1 } from "./markdown-editor-types.js";
4
+ import { joinLeadingHeading, normalizeMarkdownTitle, splitLeadingHeading } from "./markdown-title.js";
5
+ import { FontsOptions } from "@segnavia/frame/fonts";
6
+ //#region src/markdown-editor.d.ts
7
+ export interface SegnaviaMarkdownEditor extends SegnaviaMarkdownEditor$1 {}
8
+ export declare const SegnaviaMarkdownEditor: {
9
+ new (): SegnaviaMarkdownEditor;
10
+ };
11
+ //#endregion
12
+ export { type FontsOptions, MARKDOWN_EDITOR_COMMANDS, MARKDOWN_EDITOR_MODES, type MarkdownEditorCommand, type MarkdownEditorMode, type MarkdownEditorStateDetail, type MarkdownTitleSource, joinLeadingHeading, normalizeMarkdownTitle, splitLeadingHeading };