@viveeditor/ext-footnote 0.0.0-stage → 0.1.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 viveEditor contributors
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.
package/README.md CHANGED
@@ -1,3 +1,30 @@
1
- # Temporary Holding Version
1
+ # @viveeditor/ext-footnote
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Footnotes for viveEditor: inline notes edited in place and numbered on the published page.
4
+
5
+ Part of [viveEditor](https://github.com/mtvrkan/viveeditor#readme), a free, open-source rich text editor.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @viveeditor/ext-footnote @viveeditor/core
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { Editor } from '@viveeditor/core'
17
+ import '@viveeditor/core/style.css'
18
+ import { Footnote } from '@viveeditor/ext-footnote'
19
+ import '@viveeditor/ext-footnote/style.css'
20
+
21
+ const editor = await Editor.create(document.querySelector('#editor'), {
22
+ extensions: [Footnote],
23
+ })
24
+ ```
25
+
26
+ Notes are edited in place and keep bold, italic and links only. On published pages, `renderFootnotes(root)` from `@viveeditor/ext-footnote/render` numbers them and lists them at the end.
27
+
28
+ ## License
29
+
30
+ MIT
@@ -0,0 +1,20 @@
1
+ import { Extension } from "@viveeditor/core";
2
+ import "prosemirror-model";
3
+ import { Command } from "prosemirror-state";
4
+ import { EditorView } from "prosemirror-view";
5
+ interface FootnoteLabels {
6
+ footnote: string;
7
+ edit: string;
8
+ }
9
+ export declare const MAX_FOOTNOTE_LENGTH = 5e3;
10
+ export interface FootnoteOptions {
11
+ labels: FootnoteLabels;
12
+ }
13
+ declare module "@viveeditor/core" {
14
+ interface CommandRegistry {
15
+ insertFootnote: () => Command;
16
+ }
17
+ }
18
+ export declare const Footnote: Extension<FootnoteOptions>;
19
+ export type { FootnoteLabels };
20
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,239 @@
1
+ import { defineExtension } from "@viveeditor/core";
2
+ import { DOMParser, Fragment } from "prosemirror-model";
3
+ import { EditorState, NodeSelection, Plugin, TextSelection } from "prosemirror-state";
4
+ import { redo, undo } from "prosemirror-history";
5
+ import { keymap } from "prosemirror-keymap";
6
+ import { StepMap } from "prosemirror-transform";
7
+ import { EditorView } from "prosemirror-view";
8
+ // src/view.ts
9
+ const MAX_FOOTNOTE_LENGTH = 5e3;
10
+ const editors = /* @__PURE__ */ new WeakMap();
11
+ var FootnoteView = class {
12
+ node;
13
+ outer;
14
+ getPos;
15
+ labels;
16
+ dom;
17
+ inner = null;
18
+ popup = null;
19
+ constructor(node, outer, getPos, labels) {
20
+ this.node = node;
21
+ this.outer = outer;
22
+ this.getPos = getPos;
23
+ this.labels = labels;
24
+ this.dom = document.createElement("sup");
25
+ this.dom.className = "vive-footnote";
26
+ this.dom.dataset.footnote = "";
27
+ this.describe();
28
+ }
29
+ update(node) {
30
+ if (!node.sameMarkup(this.node)) return false;
31
+ this.node = node;
32
+ this.describe();
33
+ const inner = this.inner;
34
+ if (inner) {
35
+ const state = inner.state;
36
+ const start = node.content.findDiffStart(state.doc.content);
37
+ if (start != null) {
38
+ let { a: endA, b: endB } = node.content.findDiffEnd(state.doc.content) ?? {
39
+ a: start,
40
+ b: start
41
+ };
42
+ const overlap = start - Math.min(endA, endB);
43
+ if (overlap > 0) {
44
+ endA += overlap;
45
+ endB += overlap;
46
+ }
47
+ inner.dispatch(state.tr.replace(start, endB, node.slice(start, endA)).setMeta("fromOutside", true));
48
+ }
49
+ }
50
+ return true;
51
+ }
52
+ selectNode() {
53
+ this.dom.classList.add("ProseMirror-selectednode");
54
+ if (this.outer.editable) this.open();
55
+ }
56
+ deselectNode() {
57
+ this.dom.classList.remove("ProseMirror-selectednode");
58
+ this.close();
59
+ }
60
+ stopEvent(event) {
61
+ return Boolean(this.popup && event.target instanceof Node && this.popup.contains(event.target));
62
+ }
63
+ ignoreMutation() {
64
+ return true;
65
+ }
66
+ destroy() {
67
+ this.close();
68
+ }
69
+ describe() {
70
+ const text = this.node.textContent.replace(/\s+/g, " ").trim();
71
+ this.dom.setAttribute("aria-label", `${this.labels.footnote}: ${Array.from(text).slice(0, 100).join("")}`);
72
+ }
73
+ open() {
74
+ if (this.popup) return;
75
+ const popup = document.createElement("span");
76
+ this.dom.append(popup);
77
+ this.popup = popup;
78
+ const outer = this.outer;
79
+ const inner = new EditorView({ mount: popup }, {
80
+ state: EditorState.create({
81
+ doc: this.node,
82
+ plugins: [keymap({
83
+ "Mod-z": () => undo(outer.state, outer.dispatch),
84
+ "Mod-y": () => redo(outer.state, outer.dispatch),
85
+ "Shift-Mod-z": () => redo(outer.state, outer.dispatch),
86
+ Escape: () => this.leave(),
87
+ Enter: () => this.leave()
88
+ })]
89
+ }),
90
+ editable: () => outer.editable,
91
+ attributes: {
92
+ role: "textbox",
93
+ "aria-label": this.labels.edit,
94
+ class: "vive-footnote-popup vive-footnote-field"
95
+ },
96
+ transformPastedHTML: (html) => outer.someProp("transformPastedHTML", (transform) => transform(html, outer)) ?? html,
97
+ dispatchTransaction: (transaction) => this.dispatchInner(transaction)
98
+ });
99
+ this.inner = inner;
100
+ editors.set(inner.dom, inner);
101
+ inner.focus();
102
+ }
103
+ dispatchInner(transaction) {
104
+ const inner = this.inner;
105
+ if (!inner) return;
106
+ if (transaction.doc.textContent.length > 5e3 && transaction.docChanged) return;
107
+ const { state, transactions } = inner.state.applyTransaction(transaction);
108
+ inner.updateState(state);
109
+ if (transaction.getMeta("fromOutside")) return;
110
+ const pos = this.getPos();
111
+ if (pos === void 0 || !this.outer.editable) return;
112
+ const outerTransaction = this.outer.state.tr;
113
+ const offset = StepMap.offset(pos + 1);
114
+ for (const applied of transactions) for (const step of applied.steps) {
115
+ const mapped = step.map(offset);
116
+ if (mapped) outerTransaction.step(mapped);
117
+ }
118
+ if (outerTransaction.docChanged) this.outer.dispatch(outerTransaction);
119
+ }
120
+ leave() {
121
+ const pos = this.getPos();
122
+ if (pos === void 0) return false;
123
+ const { state } = this.outer;
124
+ const after = pos + this.node.nodeSize;
125
+ this.outer.dispatch(state.tr.setSelection(TextSelection.create(state.doc, after)));
126
+ this.outer.focus();
127
+ return true;
128
+ }
129
+ close() {
130
+ this.inner?.destroy();
131
+ this.inner = null;
132
+ this.popup?.remove();
133
+ this.popup = null;
134
+ }
135
+ };
136
+ // src/index.ts
137
+ const ALLOWED_MARKS = /* @__PURE__ */ new Set([
138
+ "bold",
139
+ "italic",
140
+ "link"
141
+ ]);
142
+ const keepMarks = (marks) => marks.filter((mark) => ALLOWED_MARKS.has(mark.type.name));
143
+ function footnoteContent(dom, schema) {
144
+ const parsed = DOMParser.fromSchema(schema).parseSlice(dom).content;
145
+ const texts = [];
146
+ let length = 0;
147
+ parsed.descendants((node) => {
148
+ if (length >= 5e3) return false;
149
+ if (!node.isText && !node.isLeaf) return true;
150
+ const text = ((node.isText ? node.text : node.textContent) ?? "").slice(0, MAX_FOOTNOTE_LENGTH - length);
151
+ if (text) {
152
+ texts.push(schema.text(text, node.isText ? keepMarks(node.marks) : []));
153
+ length += text.length;
154
+ }
155
+ return false;
156
+ });
157
+ return Fragment.fromArray(texts);
158
+ }
159
+ const footnoteSpec = () => ({
160
+ group: "inline",
161
+ inline: true,
162
+ atom: true,
163
+ content: "text*",
164
+ marks: "_",
165
+ parseDOM: [{
166
+ tag: "sup[data-footnote]",
167
+ priority: 60,
168
+ getContent: (dom, schema) => footnoteContent(dom, schema)
169
+ }],
170
+ toDOM: () => [
171
+ "sup",
172
+ { "data-footnote": "" },
173
+ 0
174
+ ]
175
+ });
176
+ function strayMarks(doc, type) {
177
+ const found = [];
178
+ doc.descendants((node, pos) => {
179
+ if (node.type !== type) return true;
180
+ for (const mark of node.marks) if (mark.type.name === "link") found.push([
181
+ pos,
182
+ pos + node.nodeSize,
183
+ mark
184
+ ]);
185
+ node.forEach((child, offset) => {
186
+ for (const mark of child.marks) if (!ALLOWED_MARKS.has(mark.type.name)) {
187
+ const from = pos + 1 + offset;
188
+ found.push([
189
+ from,
190
+ from + child.nodeSize,
191
+ mark
192
+ ]);
193
+ }
194
+ });
195
+ return false;
196
+ });
197
+ return found;
198
+ }
199
+ const Footnote = defineExtension({
200
+ name: "footnote",
201
+ defaultOptions: { labels: {
202
+ footnote: "Footnote",
203
+ edit: "Footnote text"
204
+ } },
205
+ nodes: () => ({ footnote: footnoteSpec() }),
206
+ commands: ({ schema }) => {
207
+ const type = schema.nodes.footnote;
208
+ return { insertFootnote: () => (state, dispatch, view) => {
209
+ if (view && !view.editable) return false;
210
+ const { $from } = state.selection;
211
+ if (!$from.parent.inlineContent || $from.parent.type.spec.code) return false;
212
+ if (!$from.parent.canReplaceWith($from.index(), $from.indexAfter(), type)) return false;
213
+ if ($from.marks().some((mark) => mark.type.name === "link")) return false;
214
+ if (dispatch) {
215
+ const transaction = state.tr.replaceSelectionWith(type.create(), false);
216
+ const pos = transaction.mapping.map(state.selection.from, -1);
217
+ dispatch(transaction.setSelection(NodeSelection.create(transaction.doc, pos)).scrollIntoView());
218
+ }
219
+ return true;
220
+ } };
221
+ },
222
+ plugins: ({ schema, options }) => {
223
+ const type = schema.nodes.footnote;
224
+ return [new Plugin({
225
+ props: { nodeViews: { footnote: (node, view, getPos) => new FootnoteView(node, view, getPos, options.labels) } },
226
+ appendTransaction: (transactions, _old, state) => {
227
+ if (!transactions.some((transaction) => transaction.docChanged)) return null;
228
+ const marks = strayMarks(state.doc, type);
229
+ if (marks.length === 0) return null;
230
+ const transaction = state.tr;
231
+ for (const [from, to, mark] of marks) transaction.removeMark(from, to, mark);
232
+ return transaction;
233
+ }
234
+ })];
235
+ }
236
+ });
237
+ export { Footnote, MAX_FOOTNOTE_LENGTH };
238
+
239
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["ContentParser"],"sources":["../src/view.ts","../src/index.ts"],"sourcesContent":["import { redo, undo } from 'prosemirror-history'\nimport { keymap } from 'prosemirror-keymap'\nimport type { Node as ProseMirrorNode } from 'prosemirror-model'\nimport { EditorState, TextSelection, type Transaction } from 'prosemirror-state'\nimport { StepMap } from 'prosemirror-transform'\nimport { EditorView, type NodeView } from 'prosemirror-view'\n\nexport interface FootnoteLabels {\n footnote: string\n edit: string\n}\n\nexport const MAX_FOOTNOTE_LENGTH = 5000\n\nconst editors = new WeakMap<Element, EditorView>()\n\nexport const footnoteEditorOf = (dom: Element | null): EditorView | undefined =>\n dom ? editors.get(dom) : undefined\n\nexport class FootnoteView implements NodeView {\n readonly dom: HTMLElement\n private inner: EditorView | null = null\n private popup: HTMLElement | null = null\n\n constructor(\n private node: ProseMirrorNode,\n private readonly outer: EditorView,\n private readonly getPos: () => number | undefined,\n private readonly labels: FootnoteLabels,\n ) {\n this.dom = document.createElement('sup')\n this.dom.className = 'vive-footnote'\n this.dom.dataset.footnote = ''\n this.describe()\n }\n\n update(node: ProseMirrorNode): boolean {\n if (!node.sameMarkup(this.node)) return false\n this.node = node\n this.describe()\n const inner = this.inner\n if (inner) {\n const state = inner.state\n const start = node.content.findDiffStart(state.doc.content)\n if (start != null) {\n const ends = node.content.findDiffEnd(state.doc.content)\n let { a: endA, b: endB } = ends ?? { a: start, b: start }\n const overlap = start - Math.min(endA, endB)\n if (overlap > 0) {\n endA += overlap\n endB += overlap\n }\n inner.dispatch(\n state.tr.replace(start, endB, node.slice(start, endA)).setMeta('fromOutside', true),\n )\n }\n }\n return true\n }\n\n selectNode(): void {\n this.dom.classList.add('ProseMirror-selectednode')\n if (this.outer.editable) this.open()\n }\n\n deselectNode(): void {\n this.dom.classList.remove('ProseMirror-selectednode')\n this.close()\n }\n\n stopEvent(event: Event): boolean {\n return Boolean(this.popup && event.target instanceof Node && this.popup.contains(event.target))\n }\n\n ignoreMutation(): boolean {\n return true\n }\n\n destroy(): void {\n this.close()\n }\n\n private describe(): void {\n const text = this.node.textContent.replace(/\\s+/g, ' ').trim()\n this.dom.setAttribute(\n 'aria-label',\n `${this.labels.footnote}: ${Array.from(text).slice(0, 100).join('')}`,\n )\n }\n\n private open(): void {\n if (this.popup) return\n const popup = document.createElement('span')\n this.dom.append(popup)\n this.popup = popup\n const outer = this.outer\n const inner = new EditorView(\n { mount: popup },\n {\n state: EditorState.create({\n doc: this.node,\n plugins: [\n keymap({\n 'Mod-z': () => undo(outer.state, outer.dispatch),\n 'Mod-y': () => redo(outer.state, outer.dispatch),\n 'Shift-Mod-z': () => redo(outer.state, outer.dispatch),\n Escape: () => this.leave(),\n Enter: () => this.leave(),\n }),\n ],\n }),\n editable: () => outer.editable,\n attributes: {\n role: 'textbox',\n 'aria-label': this.labels.edit,\n class: 'vive-footnote-popup vive-footnote-field',\n },\n transformPastedHTML: (html) =>\n outer.someProp('transformPastedHTML', (transform) => transform(html, outer)) ?? html,\n dispatchTransaction: (transaction) => this.dispatchInner(transaction),\n },\n )\n this.inner = inner\n editors.set(inner.dom, inner)\n inner.focus()\n }\n\n private dispatchInner(transaction: Transaction): void {\n const inner = this.inner\n if (!inner) return\n if (transaction.doc.textContent.length > MAX_FOOTNOTE_LENGTH && transaction.docChanged) return\n const { state, transactions } = inner.state.applyTransaction(transaction)\n inner.updateState(state)\n if (transaction.getMeta('fromOutside')) return\n const pos = this.getPos()\n if (pos === undefined || !this.outer.editable) return\n const outerTransaction = this.outer.state.tr\n const offset = StepMap.offset(pos + 1)\n for (const applied of transactions) {\n for (const step of applied.steps) {\n const mapped = step.map(offset)\n if (mapped) outerTransaction.step(mapped)\n }\n }\n if (outerTransaction.docChanged) this.outer.dispatch(outerTransaction)\n }\n\n private leave(): boolean {\n const pos = this.getPos()\n if (pos === undefined) return false\n const { state } = this.outer\n const after = pos + this.node.nodeSize\n this.outer.dispatch(state.tr.setSelection(TextSelection.create(state.doc, after)))\n this.outer.focus()\n return true\n }\n\n private close(): void {\n this.inner?.destroy()\n this.inner = null\n this.popup?.remove()\n this.popup = null\n }\n}\n","import { defineExtension, type Extension } from '@viveeditor/core'\nimport {\n DOMParser as ContentParser,\n Fragment,\n type Mark,\n type NodeSpec,\n type NodeType,\n type Node as ProseMirrorNode,\n type Schema,\n} from 'prosemirror-model'\nimport { type Command, NodeSelection, Plugin } from 'prosemirror-state'\nimport { type FootnoteLabels, FootnoteView, MAX_FOOTNOTE_LENGTH } from './view.js'\n\nexport type { FootnoteLabels } from './view.js'\nexport { MAX_FOOTNOTE_LENGTH } from './view.js'\n\nexport interface FootnoteOptions {\n labels: FootnoteLabels\n}\n\ndeclare module '@viveeditor/core' {\n interface CommandRegistry {\n insertFootnote: () => Command\n }\n}\n\nconst ALLOWED_MARKS = new Set(['bold', 'italic', 'link'])\n\nconst keepMarks = (marks: readonly Mark[]): readonly Mark[] =>\n marks.filter((mark) => ALLOWED_MARKS.has(mark.type.name))\n\nfunction footnoteContent(dom: HTMLElement, schema: Schema): Fragment {\n const parsed = ContentParser.fromSchema(schema).parseSlice(dom).content\n const texts: ProseMirrorNode[] = []\n let length = 0\n parsed.descendants((node) => {\n if (length >= MAX_FOOTNOTE_LENGTH) return false\n if (!node.isText && !node.isLeaf) return true\n const value = (node.isText ? node.text : node.textContent) ?? ''\n const text = value.slice(0, MAX_FOOTNOTE_LENGTH - length)\n if (text) {\n texts.push(schema.text(text, node.isText ? keepMarks(node.marks) : []))\n length += text.length\n }\n return false\n })\n return Fragment.fromArray(texts)\n}\n\nconst footnoteSpec = (): NodeSpec => ({\n group: 'inline',\n inline: true,\n atom: true,\n content: 'text*',\n marks: '_',\n parseDOM: [\n {\n tag: 'sup[data-footnote]',\n priority: 60,\n getContent: (dom, schema) => footnoteContent(dom as HTMLElement, schema),\n },\n ],\n toDOM: () => ['sup', { 'data-footnote': '' }, 0],\n})\n\nfunction strayMarks(doc: ProseMirrorNode, type: NodeType): Array<[number, number, Mark]> {\n const found: Array<[number, number, Mark]> = []\n doc.descendants((node, pos) => {\n if (node.type !== type) return true\n for (const mark of node.marks) {\n if (mark.type.name === 'link') found.push([pos, pos + node.nodeSize, mark])\n }\n node.forEach((child, offset) => {\n for (const mark of child.marks) {\n if (!ALLOWED_MARKS.has(mark.type.name)) {\n const from = pos + 1 + offset\n found.push([from, from + child.nodeSize, mark])\n }\n }\n })\n return false\n })\n return found\n}\n\nexport const Footnote: Extension<FootnoteOptions> = defineExtension<FootnoteOptions>({\n name: 'footnote',\n defaultOptions: { labels: { footnote: 'Footnote', edit: 'Footnote text' } },\n nodes: () => ({ footnote: footnoteSpec() }),\n commands: ({ schema }) => {\n const type = schema.nodes.footnote as NodeType\n return {\n insertFootnote: (): Command => (state, dispatch, view) => {\n if (view && !view.editable) return false\n const { $from } = state.selection\n if (!$from.parent.inlineContent || $from.parent.type.spec.code) return false\n if (!$from.parent.canReplaceWith($from.index(), $from.indexAfter(), type)) return false\n if ($from.marks().some((mark) => mark.type.name === 'link')) return false\n if (dispatch) {\n const transaction = state.tr.replaceSelectionWith(type.create(), false)\n const pos = transaction.mapping.map(state.selection.from, -1)\n dispatch(\n transaction.setSelection(NodeSelection.create(transaction.doc, pos)).scrollIntoView(),\n )\n }\n return true\n },\n }\n },\n plugins: ({ schema, options }) => {\n const type = schema.nodes.footnote as NodeType\n return [\n new Plugin({\n props: {\n nodeViews: {\n footnote: (node, view, getPos) => new FootnoteView(node, view, getPos, options.labels),\n },\n },\n appendTransaction: (transactions, _old, state) => {\n if (!transactions.some((transaction) => transaction.docChanged)) return null\n const marks = strayMarks(state.doc, type)\n if (marks.length === 0) return null\n const transaction = state.tr\n for (const [from, to, mark] of marks) transaction.removeMark(from, to, mark)\n return transaction\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;;;;;AAYA,MAAa,sBAAsB;AAEnC,MAAM,0BAAU,IAAI,QAA6B;AAKjD,IAAa,eAAb,MAA8C;CAMlC;CACS;CACA;CACA;CARnB;CACA,QAAmC;CACnC,QAAoC;CAEpC,YACE,MACA,OACA,QACA,QACA;EAJQ,KAAA,OAAA;EACS,KAAA,QAAA;EACA,KAAA,SAAA;EACA,KAAA,SAAA;EAEjB,KAAK,MAAM,SAAS,cAAc,KAAK;EACvC,KAAK,IAAI,YAAY;EACrB,KAAK,IAAI,QAAQ,WAAW;EAC5B,KAAK,SAAS;CAChB;CAEA,OAAO,MAAgC;EACrC,IAAI,CAAC,KAAK,WAAW,KAAK,IAAI,GAAG,OAAO;EACxC,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,MAAM,QAAQ,KAAK;EACnB,IAAI,OAAO;GACT,MAAM,QAAQ,MAAM;GACpB,MAAM,QAAQ,KAAK,QAAQ,cAAc,MAAM,IAAI,OAAO;GAC1D,IAAI,SAAS,MAAM;IAEjB,IAAI,EAAE,GAAG,MAAM,GAAG,SADL,KAAK,QAAQ,YAAY,MAAM,IAAI,OAClB,KAAK;KAAE,GAAG;KAAO,GAAG;IAAM;IACxD,MAAM,UAAU,QAAQ,KAAK,IAAI,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WAAW,EACT,WAAW,MAAM,MAAM,WAAW,IAAI,aAAa,MAAM,MAAM,QAAQ,QAAQ,MAAM,EACvF,EACF;GACA,oBAAoB,cAAc,MAAM,UAAU;IAChD,IAAI,CAAC,aAAa,MAAM,gBAAgB,YAAY,UAAU,GAAG,OAAO;IACxE,MAAM,QAAQ,WAAW,MAAM,KAAK,IAAI;IACxC,IAAI,MAAM,WAAW,GAAG,OAAO;IAC/B,MAAM,cAAc,MAAM;IAC1B,KAAK,MAAM,CAAC,MAAM,IAAI,SAAS,OAAO,YAAY,WAAW,MAAM,IAAI,IAAI;IAC3E,OAAO;GACT;EACF,CAAC,CACH;CACF;AACF,CAAC"}
@@ -0,0 +1,5 @@
1
+ export interface RenderFootnotesOptions {
2
+ backLabel?: string;
3
+ }
4
+ export declare function renderFootnotes(root: ParentNode, options?: RenderFootnotesOptions): number;
5
+ //# sourceMappingURL=render.d.ts.map
package/dist/render.js ADDED
@@ -0,0 +1,38 @@
1
+ // src/render.ts
2
+ let calls = 0;
3
+ function renderFootnotes(root, options = {}) {
4
+ const notes = Array.from(root.querySelectorAll("sup[data-footnote]:not([data-rendered])"));
5
+ if (notes.length === 0) return 0;
6
+ const prefix = `vive-fn-${++calls}`;
7
+ const owner = root.ownerDocument ?? document;
8
+ const list = owner.createElement("ol");
9
+ list.className = "vive-footnotes";
10
+ notes.forEach((note, index) => {
11
+ const number = String(index + 1);
12
+ const item = owner.createElement("li");
13
+ item.id = `${prefix}-${number}`;
14
+ item.append(...Array.from(note.childNodes));
15
+ const reference = `${prefix}-ref-${number}`;
16
+ if (note.closest("a")) note.textContent = number;
17
+ else {
18
+ const link = owner.createElement("a");
19
+ link.href = `#${item.id}`;
20
+ link.id = reference;
21
+ link.textContent = number;
22
+ note.replaceChildren(link);
23
+ const back = owner.createElement("a");
24
+ back.href = `#${reference}`;
25
+ back.className = "vive-footnote-back";
26
+ back.textContent = "↩";
27
+ back.setAttribute("aria-label", options.backLabel ?? "Back to text");
28
+ item.append(" ", back);
29
+ }
30
+ note.dataset.rendered = "";
31
+ list.append(item);
32
+ });
33
+ root.append(list);
34
+ return notes.length;
35
+ }
36
+ export { renderFootnotes };
37
+
38
+ //# sourceMappingURL=render.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.js","names":[],"sources":["../src/render.ts"],"sourcesContent":["export interface RenderFootnotesOptions {\n backLabel?: string\n}\n\nlet calls = 0\n\nexport function renderFootnotes(root: ParentNode, options: RenderFootnotesOptions = {}): number {\n const notes = Array.from(\n root.querySelectorAll<HTMLElement>('sup[data-footnote]:not([data-rendered])'),\n )\n if (notes.length === 0) return 0\n const prefix = `vive-fn-${++calls}`\n const owner = (root as Node).ownerDocument ?? document\n const list = owner.createElement('ol')\n list.className = 'vive-footnotes'\n notes.forEach((note, index) => {\n const number = String(index + 1)\n const item = owner.createElement('li')\n item.id = `${prefix}-${number}`\n item.append(...Array.from(note.childNodes))\n const reference = `${prefix}-ref-${number}`\n if (note.closest('a')) {\n note.textContent = number\n } else {\n const link = owner.createElement('a')\n link.href = `#${item.id}`\n link.id = reference\n link.textContent = number\n note.replaceChildren(link)\n const back = owner.createElement('a')\n back.href = `#${reference}`\n back.className = 'vive-footnote-back'\n back.textContent = '↩'\n back.setAttribute('aria-label', options.backLabel ?? 'Back to text')\n item.append(' ', back)\n }\n note.dataset.rendered = ''\n list.append(item)\n })\n root.append(list)\n return notes.length\n}\n"],"mappings":";AAIA,IAAI,QAAQ;AAEZ,SAAgB,gBAAgB,MAAkB,UAAkC,CAAC,GAAW;CAC9F,MAAM,QAAQ,MAAM,KAClB,KAAK,iBAA8B,yCAAyC,CAC9E;CACA,IAAI,MAAM,WAAW,GAAG,OAAO;CAC/B,MAAM,SAAS,WAAW,EAAE;CAC5B,MAAM,QAAS,KAAc,iBAAiB;CAC9C,MAAM,OAAO,MAAM,cAAc,IAAI;CACrC,KAAK,YAAY;CACjB,MAAM,SAAS,MAAM,UAAU;EAC7B,MAAM,SAAS,OAAO,QAAQ,CAAC;EAC/B,MAAM,OAAO,MAAM,cAAc,IAAI;EACrC,KAAK,KAAK,GAAG,OAAO,GAAG;EACvB,KAAK,OAAO,GAAG,MAAM,KAAK,KAAK,UAAU,CAAC;EAC1C,MAAM,YAAY,GAAG,OAAO,OAAO;EACnC,IAAI,KAAK,QAAQ,GAAG,GAClB,KAAK,cAAc;OACd;GACL,MAAM,OAAO,MAAM,cAAc,GAAG;GACpC,KAAK,OAAO,IAAI,KAAK;GACrB,KAAK,KAAK;GACV,KAAK,cAAc;GACnB,KAAK,gBAAgB,IAAI;GACzB,MAAM,OAAO,MAAM,cAAc,GAAG;GACpC,KAAK,OAAO,IAAI;GAChB,KAAK,YAAY;GACjB,KAAK,cAAc;GACnB,KAAK,aAAa,cAAc,QAAQ,aAAa,cAAc;GACnE,KAAK,OAAO,KAAK,IAAI;EACvB;EACA,KAAK,QAAQ,WAAW;EACxB,KAAK,OAAO,IAAI;CAClB,CAAC;CACD,KAAK,OAAO,IAAI;CAChB,OAAO,MAAM;AACf"}
package/package.json CHANGED
@@ -1,6 +1,96 @@
1
1
  {
2
2
  "name": "@viveeditor/ext-footnote",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Footnotes for viveEditor: inline notes edited in place and numbered on the published page.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "viveeditor",
8
+ "rich-text-editor",
9
+ "wysiwyg",
10
+ "editor",
11
+ "prosemirror",
12
+ "footnote"
13
+ ],
14
+ "homepage": "https://github.com/mtvrkan/viveeditor/tree/main/packages/ext-footnote#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/mtvrkan/viveeditor/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/mtvrkan/viveeditor.git",
21
+ "directory": "packages/ext-footnote"
22
+ },
23
+ "type": "module",
24
+ "sideEffects": [
25
+ "*.css"
26
+ ],
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./render": {
33
+ "types": "./dist/render.d.ts",
34
+ "import": "./dist/render.js"
35
+ },
36
+ "./style.css": "./style.css"
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "style.css"
41
+ ],
42
+ "peerDependencies": {
43
+ "@viveeditor/core": "^0.1.0"
44
+ },
45
+ "dependencies": {
46
+ "prosemirror-history": "^1.5.1",
47
+ "prosemirror-keymap": "^1.2.3",
48
+ "prosemirror-model": "^1.25.12",
49
+ "prosemirror-state": "^1.4.4",
50
+ "prosemirror-transform": "^1.12.4",
51
+ "prosemirror-view": "^1.42.7"
52
+ },
53
+ "devDependencies": {
54
+ "@size-limit/preset-small-lib": "^14.3.0",
55
+ "jsdom": "^30.1.3",
56
+ "size-limit": "^14.3.0",
57
+ "tsdown": "0.23.1",
58
+ "vitest": "^5.0.5",
59
+ "@viveeditor/ext-basic": "0.1.0",
60
+ "@viveeditor/core": "0.1.0"
61
+ },
62
+ "size-limit": [
63
+ {
64
+ "name": "@viveeditor/ext-footnote",
65
+ "path": "dist/index.js",
66
+ "ignore": [
67
+ "@viveeditor/core",
68
+ "prosemirror-history",
69
+ "prosemirror-keymap",
70
+ "prosemirror-model",
71
+ "prosemirror-state",
72
+ "prosemirror-transform",
73
+ "prosemirror-view"
74
+ ],
75
+ "limit": "3 KB"
76
+ },
77
+ {
78
+ "name": "@viveeditor/ext-footnote/render",
79
+ "path": "dist/render.js",
80
+ "limit": "1 KB"
81
+ }
82
+ ],
83
+ "engines": {
84
+ "node": ">=22.12"
85
+ },
86
+ "publishConfig": {
87
+ "access": "public",
88
+ "provenance": true
89
+ },
90
+ "scripts": {
91
+ "build": "tsdown",
92
+ "typecheck": "tsc -p tsconfig.json",
93
+ "test": "vitest run",
94
+ "size": "size-limit"
95
+ }
6
96
  }
package/style.css ADDED
@@ -0,0 +1,57 @@
1
+ .vive-content {
2
+ counter-reset: vive-footnote;
3
+ }
4
+
5
+ .vive-content .vive-footnote {
6
+ position: relative;
7
+ counter-increment: vive-footnote;
8
+ cursor: pointer;
9
+ }
10
+
11
+ .vive-content .vive-footnote::before {
12
+ content: "[" counter(vive-footnote) "]";
13
+ color: var(--ve-primary, hsl(221 83% 53%));
14
+ }
15
+
16
+ .vive-content .vive-footnote.ProseMirror-selectednode {
17
+ outline: 2px solid var(--ve-primary, hsl(221 83% 53%));
18
+ outline-offset: 1px;
19
+ border-radius: 3px;
20
+ }
21
+
22
+ .vive-content .vive-footnote-popup {
23
+ position: absolute;
24
+ inset-inline-start: 50%;
25
+ top: calc(100% + 6px);
26
+ z-index: 10;
27
+ width: max-content;
28
+ max-width: min(28rem, 80vw);
29
+ translate: -50% 0;
30
+ padding: 6px 10px;
31
+ border: 1px solid var(--ve-border, hsl(220 13% 85%));
32
+ border-radius: 8px;
33
+ background: var(--ve-bg, hsl(0 0% 100%));
34
+ box-shadow: 0 8px 24px hsl(220 30% 10% / 0.14);
35
+ color: var(--ve-fg, hsl(222 24% 13%));
36
+ font-size: 14px;
37
+ line-height: 1.5;
38
+ vertical-align: baseline;
39
+ cursor: text;
40
+ }
41
+
42
+ .vive-content .vive-footnote-field {
43
+ min-width: 10ch;
44
+ outline: none;
45
+ white-space: pre-wrap;
46
+ }
47
+
48
+ .vive-footnotes {
49
+ margin-top: 2em;
50
+ padding-top: 0.6em;
51
+ border-top: 1px solid var(--ve-border, hsl(220 13% 85%));
52
+ font-size: 0.9em;
53
+ }
54
+
55
+ .vive-footnote-back {
56
+ text-decoration: none;
57
+ }