@memorysmithapp/markdown-renderer 0.12.0 → 0.13.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/README.md +8 -2
- package/dist/ContentsBlock.d.ts +82 -0
- package/dist/ContentsBlock.js +162 -0
- package/dist/DataTable.js +31 -4
- package/dist/InPageAnchor.d.ts +20 -0
- package/dist/InPageAnchor.js +35 -0
- package/dist/Markdown.d.ts +7 -1
- package/dist/Markdown.js +48 -31
- package/dist/NoteBody.js +107 -28
- package/dist/PropertiesBlock.d.ts +54 -3
- package/dist/PropertiesBlock.js +91 -5
- package/dist/Transclusion.js +4 -1
- package/dist/appendices.d.ts +30 -0
- package/dist/appendices.js +11 -4
- package/dist/contents.d.ts +92 -0
- package/dist/contents.js +224 -0
- package/dist/diagnose.d.ts +3 -1
- package/dist/diagnose.js +25 -0
- package/dist/edit/ContentsDialog.d.ts +39 -0
- package/dist/edit/ContentsDialog.js +126 -0
- package/dist/edit/NoteEditor.js +56 -4
- package/dist/edit/PropertiesDialog.d.ts +42 -0
- package/dist/edit/PropertiesDialog.js +103 -0
- package/dist/edit/StyleDialog.js +5 -3
- package/dist/edit/blocks.d.ts +29 -0
- package/dist/edit/blocks.js +100 -3
- package/dist/edit/contents.d.ts +30 -0
- package/dist/edit/contents.js +76 -0
- package/dist/edit/live.js +72 -2
- package/dist/edit/properties.d.ts +26 -0
- package/dist/edit/properties.js +60 -0
- package/dist/host.d.ts +24 -0
- package/dist/host.js +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/kept-files.d.ts +11 -0
- package/dist/kept-files.js +49 -0
- package/dist/note-style.d.ts +9 -1
- package/dist/note-style.js +4 -2
- package/dist/outline.d.ts +227 -0
- package/dist/outline.js +366 -0
- package/dist/properties-block.d.ts +62 -0
- package/dist/properties-block.js +175 -0
- package/dist/slots.js +11 -2
- package/dist/strings.d.ts +60 -0
- package/dist/strings.js +43 -1
- package/dist/table-block.d.ts +13 -1
- package/dist/table-block.js +2 -1
- package/dist/theme.d.ts +19 -0
- package/package.json +4 -2
- package/structure.css +54 -0
- package/theme-default.css +56 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The form of a `properties` block (`SPEC.md` §7.18, #172): which of the note's keys it draws and in
|
|
4
|
+
* which order, its title, whether it starts folded, where it is drawn, and what the screen and print
|
|
5
|
+
* draw — the same, nothing, or their own. A field left at the notebook's writes nothing. It inserts a
|
|
6
|
+
* block at the cursor, or changes the block the cursor is in; the editor writes the bytes.
|
|
7
|
+
*/
|
|
8
|
+
import { useEffect, useId, useRef, useState } from 'react';
|
|
9
|
+
import { useHost } from '../host.js';
|
|
10
|
+
import { CONTENTS_MEDIA } from '../contents.js';
|
|
11
|
+
import { ICONS } from './blocks.js';
|
|
12
|
+
function fieldsOf(layer, noteKeys) {
|
|
13
|
+
const named = layer?.keys ?? [];
|
|
14
|
+
const rest = noteKeys.filter((key) => !named.includes(key));
|
|
15
|
+
return {
|
|
16
|
+
keys: [...named.map((key) => ({ key, chosen: true })), ...rest.map((key) => ({ key, chosen: false }))],
|
|
17
|
+
some: named.length > 0,
|
|
18
|
+
title: layer?.title ?? '',
|
|
19
|
+
fold: layer?.fold ?? '',
|
|
20
|
+
place: layer?.place ?? '',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** The form's fields, from what a block sets and the keys the note has. */
|
|
24
|
+
export function propertiesFields(settings, noteKeys) {
|
|
25
|
+
const medium = (name) => {
|
|
26
|
+
const layer = settings[name];
|
|
27
|
+
if (layer === 'none')
|
|
28
|
+
return { mode: 'none', layer: fieldsOf(undefined, noteKeys) };
|
|
29
|
+
if (layer === undefined)
|
|
30
|
+
return { mode: 'same', layer: fieldsOf(undefined, noteKeys) };
|
|
31
|
+
return { mode: 'own', layer: fieldsOf(layer, noteKeys) };
|
|
32
|
+
};
|
|
33
|
+
return { root: fieldsOf(settings, noteKeys), media: { screen: medium('screen'), print: medium('print') } };
|
|
34
|
+
}
|
|
35
|
+
function layerOf(fields) {
|
|
36
|
+
const keys = fields.some ? fields.keys.filter((each) => each.chosen).map((each) => each.key) : [];
|
|
37
|
+
return {
|
|
38
|
+
...(keys.length > 0 ? { keys } : {}),
|
|
39
|
+
...(fields.title.trim() ? { title: fields.title.trim() } : {}),
|
|
40
|
+
...(fields.fold ? { fold: fields.fold } : {}),
|
|
41
|
+
...(fields.place ? { place: fields.place } : {}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** The settings the form's fields write. */
|
|
45
|
+
export function propertiesSettings(fields) {
|
|
46
|
+
const settings = { ...layerOf(fields.root) };
|
|
47
|
+
for (const medium of CONTENTS_MEDIA) {
|
|
48
|
+
const { mode, layer } = fields.media[medium];
|
|
49
|
+
if (mode === 'none')
|
|
50
|
+
settings[medium] = 'none';
|
|
51
|
+
else if (mode === 'own') {
|
|
52
|
+
const own = layerOf(layer);
|
|
53
|
+
if (Object.keys(own).length > 0)
|
|
54
|
+
settings[medium] = own;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return settings;
|
|
58
|
+
}
|
|
59
|
+
export function PropertiesDialog({ initial, noteKeys, editing, onApply, onClose, onClosed, }) {
|
|
60
|
+
const host = useHost();
|
|
61
|
+
const words = host.strings.editor;
|
|
62
|
+
const [fields, setFields] = useState(() => propertiesFields(initial, noteKeys));
|
|
63
|
+
const dialog = useRef(null);
|
|
64
|
+
const applied = useRef(false);
|
|
65
|
+
const closed = useRef(onClosed);
|
|
66
|
+
closed.current = onClosed;
|
|
67
|
+
const id = useId();
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
const element = dialog.current;
|
|
70
|
+
if (!element)
|
|
71
|
+
return;
|
|
72
|
+
if (typeof element.showModal === 'function')
|
|
73
|
+
element.showModal();
|
|
74
|
+
else
|
|
75
|
+
element.setAttribute('open', '');
|
|
76
|
+
element.querySelector('input, select')?.focus();
|
|
77
|
+
return () => {
|
|
78
|
+
if (element.open && typeof element.close === 'function')
|
|
79
|
+
element.close();
|
|
80
|
+
closed.current(applied.current);
|
|
81
|
+
};
|
|
82
|
+
}, []);
|
|
83
|
+
const submit = (event) => {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
applied.current = true;
|
|
86
|
+
onClose();
|
|
87
|
+
onApply(propertiesSettings(fields));
|
|
88
|
+
};
|
|
89
|
+
const layerFields = (layer, set, root) => (_jsxs(_Fragment, { children: [_jsxs("fieldset", { className: "edit-properties-keys", children: [_jsxs("label", { className: "edit-contents-check", children: [_jsx("input", { type: "checkbox", name: "all-keys", checked: !layer.some, onChange: (event) => set({ ...layer, some: !event.target.checked }) }), _jsx("span", { children: words.propertiesAllKeys })] }), layer.some &&
|
|
90
|
+
layer.keys.map((each, index) => (_jsxs("div", { className: "edit-properties-key", children: [_jsxs("label", { className: "edit-contents-check", children: [_jsx("input", { type: "checkbox", name: `key-${each.key}`, checked: each.chosen, onChange: (event) => set({ ...layer, keys: layer.keys.map((other) => (other.key === each.key ? { ...other, chosen: event.target.checked } : other)) }) }), _jsx("span", { children: each.key })] }), index > 0 && (_jsx("button", { type: "button", className: "edit-control", "aria-label": words.propertiesMoveUp(each.key), title: words.propertiesMoveUp(each.key), onClick: () => {
|
|
91
|
+
const keys = [...layer.keys];
|
|
92
|
+
[keys[index - 1], keys[index]] = [keys[index], keys[index - 1]];
|
|
93
|
+
set({ ...layer, keys });
|
|
94
|
+
}, children: "\u2191" }))] }, each.key)))] }), _jsxs("label", { children: [_jsx("span", { children: words.contentsTitle }), _jsx("input", { className: "edit-attach-input", name: "title", value: layer.title, placeholder: host.properties?.title ?? host.strings.properties, onChange: (event) => set({ ...layer, title: event.target.value }) })] }), _jsxs("label", { children: [_jsx("span", { children: words.propertiesFold }), _jsxs("select", { className: "edit-attach-input", name: "fold", value: layer.fold, onChange: (event) => set({ ...layer, fold: event.target.value }), children: [_jsx("option", { value: "", children: words.contentsNotebook }), _jsx("option", { value: "open", children: words.propertiesOpen }), _jsx("option", { value: "closed", children: words.propertiesClosed })] })] }), _jsxs("label", { children: [_jsx("span", { children: words.contentsPlace }), _jsxs("select", { className: "edit-attach-input", name: "place", value: layer.place, onChange: (event) => set({ ...layer, place: event.target.value }), children: [_jsx("option", { value: "", children: root ? words.contentsHere : words.contentsSame }), _jsx("option", { value: "start", children: words.contentsStart }), _jsx("option", { value: "end", children: words.contentsEnd })] })] })] }));
|
|
95
|
+
return (_jsx("dialog", { ref: dialog, className: "edit-source edit-attach edit-style edit-contents edit-properties", "aria-modal": "true", "aria-labelledby": `${id}-title`, onCancel: (event) => {
|
|
96
|
+
event.preventDefault();
|
|
97
|
+
onClose();
|
|
98
|
+
}, onClick: (event) => event.target === dialog.current && onClose(), children: _jsxs("div", { className: "edit-source-frame", children: [_jsxs("header", { className: "edit-source-header", children: [_jsx("h2", { id: `${id}-title`, className: "edit-source-title", children: words.propertiesBlock }), _jsx("button", { type: "button", className: "edit-control edit-source-close", "aria-label": host.strings.close, title: host.strings.close, onClick: onClose, children: ICONS.remove })] }), _jsx("div", { className: "edit-attach-panel", children: _jsxs("form", { className: "edit-attach-form", onSubmit: submit, noValidate: true, children: [layerFields(fields.root, (root) => setFields((current) => ({ ...current, root })), true), CONTENTS_MEDIA.map((medium) => {
|
|
99
|
+
const { mode, layer } = fields.media[medium];
|
|
100
|
+
const setMedium = (next) => setFields((current) => ({ ...current, media: { ...current.media, [medium]: next } }));
|
|
101
|
+
return (_jsxs("fieldset", { className: "edit-contents-medium", "data-medium": medium, children: [_jsxs("label", { children: [_jsx("span", { children: medium === 'screen' ? words.contentsScreen : words.contentsPrint }), _jsxs("select", { className: "edit-attach-input", name: medium, value: mode, onChange: (event) => setMedium({ mode: event.target.value, layer }), children: [_jsx("option", { value: "same", children: words.contentsSame }), _jsx("option", { value: "none", children: words.contentsNone }), _jsx("option", { value: "own", children: words.contentsOwn })] })] }), mode === 'own' && layerFields(layer, (next) => setMedium({ mode, layer: next }), false)] }, medium));
|
|
102
|
+
}), _jsx("div", { className: "edit-attach-actions", children: _jsx("button", { type: "submit", className: "edit-attach-button edit-attach-primary", children: editing ? words.contentsApply : words.contentsInsert }) })] }) })] }) }));
|
|
103
|
+
}
|
package/dist/edit/StyleDialog.js
CHANGED
|
@@ -6,7 +6,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
6
6
|
*/
|
|
7
7
|
import { useEffect, useId, useRef, useState } from 'react';
|
|
8
8
|
import { useHost } from '../host.js';
|
|
9
|
-
import { STYLE_ALIGNMENTS, STYLE_FONTS, STYLE_KEYS } from '../note-style.js';
|
|
9
|
+
import { STYLE_ALIGNMENTS, STYLE_FONTS, STYLE_KEYS, STYLE_NUMBERINGS } from '../note-style.js';
|
|
10
10
|
import { ICONS } from './blocks.js';
|
|
11
11
|
export function StyleDialog({ initial, editing, onApply, onClose, onClosed, }) {
|
|
12
12
|
const host = useHost();
|
|
@@ -32,8 +32,10 @@ export function StyleDialog({ initial, editing, onApply, onClose, onClosed, }) {
|
|
|
32
32
|
closed.current(applied.current);
|
|
33
33
|
};
|
|
34
34
|
}, []);
|
|
35
|
-
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables };
|
|
35
|
+
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables, numbering: words.styleNumbering };
|
|
36
36
|
const value = {
|
|
37
|
+
none: words.styleNumberingNone,
|
|
38
|
+
decimal: words.styleNumberingDecimal,
|
|
37
39
|
sans: words.styleSans,
|
|
38
40
|
serif: words.styleSerif,
|
|
39
41
|
left: words.styleLeft,
|
|
@@ -60,5 +62,5 @@ export function StyleDialog({ initial, editing, onApply, onClose, onClosed, }) {
|
|
|
60
62
|
delete next[key];
|
|
61
63
|
return next;
|
|
62
64
|
});
|
|
63
|
-
}, children: [_jsx("option", { value: "", children: words.styleNotebook }), (key === 'font' ? STYLE_FONTS : STYLE_ALIGNMENTS).map((each) => (_jsx("option", { value: each, children: value[each] }, each)))] })] }, key))), _jsx("div", { className: "edit-attach-actions", children: _jsx("button", { type: "submit", className: "edit-attach-button edit-attach-primary", children: editing ? words.styleApply : words.styleInsert }) })] }) })] }) }));
|
|
65
|
+
}, children: [_jsx("option", { value: "", children: words.styleNotebook }), (key === 'font' ? STYLE_FONTS : key === 'numbering' ? STYLE_NUMBERINGS : STYLE_ALIGNMENTS).map((each) => (_jsx("option", { value: each, children: value[each] }, each)))] })] }, key))), _jsx("div", { className: "edit-attach-actions", children: _jsx("button", { type: "submit", className: "edit-attach-button edit-attach-primary", children: editing ? words.styleApply : words.styleInsert }) })] }) })] }) }));
|
|
64
66
|
}
|
package/dist/edit/blocks.d.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import type { EditorView } from '@codemirror/view';
|
|
11
11
|
import { type ReactNode } from 'react';
|
|
12
12
|
import { type NoteStyle } from '../note-style.js';
|
|
13
|
+
import { type TextChange } from './table.js';
|
|
13
14
|
/**
|
|
14
15
|
* The opening of a style block while the author is elsewhere (`SPEC.md` §7.16, #166): what it sets,
|
|
15
16
|
* as the form says it, in place of its fence and options. A click shows them as written; the
|
|
@@ -80,6 +81,14 @@ export declare function TableBlock({ view, from, to }: {
|
|
|
80
81
|
from: number;
|
|
81
82
|
to: number;
|
|
82
83
|
}): import("react").JSX.Element;
|
|
84
|
+
/** The caption of the table that ends at `end`: where it is written, from the line break after the table, and its text. */
|
|
85
|
+
export declare function tableCaptionAt(source: string, end: number): {
|
|
86
|
+
from: number;
|
|
87
|
+
to: number;
|
|
88
|
+
text: string;
|
|
89
|
+
} | null;
|
|
90
|
+
/** The changes that set a table's caption to `text`: written, rewritten, or taken out when empty. */
|
|
91
|
+
export declare function setTableCaption(source: string, end: number, text: string): TextChange[];
|
|
83
92
|
/** Shows a block as its text: the cursor goes into it, and the editor draws its source. */
|
|
84
93
|
export declare function reveal(view: EditorView, at: number): void;
|
|
85
94
|
export declare function PropertiesBlock({ view, to }: {
|
|
@@ -122,6 +131,8 @@ export declare const ICONS: {
|
|
|
122
131
|
readonly diagram: ReactNode;
|
|
123
132
|
readonly rule: ReactNode;
|
|
124
133
|
readonly style: ReactNode;
|
|
134
|
+
readonly propertiesBlock: ReactNode;
|
|
135
|
+
readonly contents: ReactNode;
|
|
125
136
|
readonly footnote: ReactNode;
|
|
126
137
|
readonly comment: ReactNode;
|
|
127
138
|
readonly caret: ReactNode;
|
|
@@ -137,3 +148,21 @@ export declare const ICONS: {
|
|
|
137
148
|
readonly edit: ReactNode;
|
|
138
149
|
readonly view: ReactNode;
|
|
139
150
|
};
|
|
151
|
+
/**
|
|
152
|
+
* A `contents` block in edit mode (`SPEC.md` §7.17, #173), while the author is elsewhere: the
|
|
153
|
+
* contents it draws on the screen, over the note as it is now, or, where it draws nothing here —
|
|
154
|
+
* moved to the start or the end, or for print alone — a line saying where it is drawn.
|
|
155
|
+
*/
|
|
156
|
+
export declare function ContentsPreview({ body, ordinal }: {
|
|
157
|
+
body: string;
|
|
158
|
+
ordinal: number;
|
|
159
|
+
}): import("react").JSX.Element | null;
|
|
160
|
+
/**
|
|
161
|
+
* A `properties` block in edit mode (`SPEC.md` §7.18, #172), while the author is elsewhere: the
|
|
162
|
+
* entries it draws on the screen, from the note's frontmatter as it is now, or, where it draws
|
|
163
|
+
* nothing here, a line saying where it is drawn.
|
|
164
|
+
*/
|
|
165
|
+
export declare function PropertiesPreview({ text, ordinal }: {
|
|
166
|
+
text: string;
|
|
167
|
+
ordinal: number;
|
|
168
|
+
}): import("react").JSX.Element | null;
|
package/dist/edit/blocks.js
CHANGED
|
@@ -9,7 +9,7 @@ import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-run
|
|
|
9
9
|
* keeps a copy of the note.
|
|
10
10
|
*/
|
|
11
11
|
import { ChangeSet } from '@codemirror/state';
|
|
12
|
-
import { useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
13
13
|
import { useHost } from '../host.js';
|
|
14
14
|
import { Markdown } from '../Markdown.js';
|
|
15
15
|
import { hideComments, resolveWikilinks } from '../source.js';
|
|
@@ -19,6 +19,13 @@ import { splitEmbeds } from '../embeds.js';
|
|
|
19
19
|
import { embedsFile } from '../embed-target.js';
|
|
20
20
|
import { STYLE_KEYS } from '../note-style.js';
|
|
21
21
|
import { wikilinkAddress } from '../wikilink-address.js';
|
|
22
|
+
import { planItems, planOutline } from '../outline.js';
|
|
23
|
+
import { useKeptFiles } from '../kept-files.js';
|
|
24
|
+
import { readContents, resolveBlock } from '../contents.js';
|
|
25
|
+
import { ContentsAt, ContentsContext, listedIn } from '../ContentsBlock.js';
|
|
26
|
+
import { Properties } from '../PropertiesBlock.js';
|
|
27
|
+
import { readPropertiesBlock, resolvePropertiesBlock } from '../properties-block.js';
|
|
28
|
+
import { bodyStart } from '../source.js';
|
|
22
29
|
import { ICON_OWN_SIZE, ICON_RATIO_FREE, ICON_RATIO_LOCKED, ICON_RESIZE } from '../mode-icons.js';
|
|
23
30
|
import { describeImage, MAX_IMAGE_WIDTH, readImageSize, resizeImage } from './image-size.js';
|
|
24
31
|
import { addEntry, readFrontmatter, removeEntry, renameKey, setList, setScalar, } from './frontmatter.js';
|
|
@@ -56,8 +63,10 @@ function write(view, changes, event = 'input.form') {
|
|
|
56
63
|
*/
|
|
57
64
|
export function StyleSummary({ style }) {
|
|
58
65
|
const words = useHost().strings.editor;
|
|
59
|
-
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables };
|
|
66
|
+
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables, numbering: words.styleNumbering };
|
|
60
67
|
const value = {
|
|
68
|
+
none: words.styleNumberingNone,
|
|
69
|
+
decimal: words.styleNumberingDecimal,
|
|
61
70
|
sans: words.styleSans,
|
|
62
71
|
serif: words.styleSerif,
|
|
63
72
|
left: words.styleLeft,
|
|
@@ -768,7 +777,42 @@ export function TableBlock({ view, from, to }) {
|
|
|
768
777
|
return (_jsxs("div", { className: "edit-block edit-table", children: [_jsxs("div", { className: "edit-block-controls", "data-ms-control": "", children: [_jsx(Control, { label: words.addRow, onClick: () => act((current) => addRow(current, focus.row)), children: ICONS.rowAdd }), _jsx(Control, { label: words.removeRow, onClick: () => act((current) => removeRow(current, focus.row)), children: ICONS.rowRemove }), _jsx(Control, { label: words.addColumn, onClick: () => act((current) => addColumn(current, focus.column)), children: ICONS.columnAdd }), _jsx(Control, { label: words.removeColumn, onClick: () => act((current) => removeColumn(current, focus.column)), children: ICONS.columnRemove }), _jsx(Control, { label: words.alignLeft, pressed: table.align[focus.column] === 'left', onClick: () => align(table.align[focus.column] === 'left' ? null : 'left'), children: ICONS.alignLeft }), _jsx(Control, { label: words.alignCenter, pressed: table.align[focus.column] === 'center', onClick: () => align(table.align[focus.column] === 'center' ? null : 'center'), children: ICONS.alignCenter }), _jsx(Control, { label: words.alignRight, pressed: table.align[focus.column] === 'right', onClick: () => align(table.align[focus.column] === 'right' ? null : 'right'), children: ICONS.alignRight }), _jsx(Control, { label: words.alignJustify, pressed: table.align[focus.column] === 'justify', onClick: () => align(table.align[focus.column] === 'justify' ? null : 'justify'), children: ICONS.alignJustify }), _jsx(Control, { label: words.source, onClick: () => {
|
|
769
778
|
act(() => []);
|
|
770
779
|
reveal(view, table.from);
|
|
771
|
-
}, children: ICONS.source })] }), _jsx("div", { className: "markdown", children: _jsxs("table", { ref: root, children: [_jsx("thead", { children: _jsx("tr", { children: Array.from({ length: width }, (_, column) => cell(0, column)) }) }), bodyRows.length > 0 && (_jsx("tbody", { children: bodyRows.map((_, index) => (_jsx("tr", { children: Array.from({ length: width }, (__, column) => cell(index + 2, column)) }, index))) }))] }) })] }));
|
|
780
|
+
}, children: ICONS.source })] }), _jsx("div", { className: "markdown", children: _jsxs("table", { ref: root, children: [_jsx("thead", { children: _jsx("tr", { children: Array.from({ length: width }, (_, column) => cell(0, column)) }) }), bodyRows.length > 0 && (_jsx("tbody", { children: bodyRows.map((_, index) => (_jsx("tr", { children: Array.from({ length: width }, (__, column) => cell(index + 2, column)) }, index))) }))] }) }), _jsx(CaptionField, { view: view, end: table.to })] }));
|
|
781
|
+
}
|
|
782
|
+
/** A Markdown table's caption (#175): the `Table:` paragraph after a blank line below its last row. */
|
|
783
|
+
const CAPTION_AFTER = /^\n[ \t]*\n[ \t]*table:[ \t]*([^\n]*)/i;
|
|
784
|
+
/** The caption of the table that ends at `end`: where it is written, from the line break after the table, and its text. */
|
|
785
|
+
export function tableCaptionAt(source, end) {
|
|
786
|
+
const at = end > 0 && source[end - 1] === '\n' ? end - 1 : end;
|
|
787
|
+
const match = CAPTION_AFTER.exec(source.slice(at));
|
|
788
|
+
return match ? { from: at, to: at + match[0].length, text: match[1].trim() } : null;
|
|
789
|
+
}
|
|
790
|
+
/** The changes that set a table's caption to `text`: written, rewritten, or taken out when empty. */
|
|
791
|
+
export function setTableCaption(source, end, text) {
|
|
792
|
+
const at = end > 0 && source[end - 1] === '\n' ? end - 1 : end;
|
|
793
|
+
const caption = tableCaptionAt(source, end);
|
|
794
|
+
const written = text.trim() === '' ? '' : `\n\nTable: ${text.trim()}`;
|
|
795
|
+
if (caption)
|
|
796
|
+
return caption.text === text.trim() ? [] : [{ from: caption.from, to: caption.to, insert: written }];
|
|
797
|
+
return written ? [{ from: at, to: at, insert: written }] : [];
|
|
798
|
+
}
|
|
799
|
+
/** The field under a table that writes its caption, so the author never types `Table:` (#175). */
|
|
800
|
+
function CaptionField({ view, end }) {
|
|
801
|
+
const { strings } = useHost();
|
|
802
|
+
const source = view.state.doc.sliceString(0, view.state.doc.length, '\n');
|
|
803
|
+
const written = tableCaptionAt(source, end)?.text ?? '';
|
|
804
|
+
const [value, setValue] = useState(written);
|
|
805
|
+
useEffect(() => setValue(written), [written]);
|
|
806
|
+
const commit = () => {
|
|
807
|
+
const now = view.state.doc.sliceString(0, view.state.doc.length, '\n');
|
|
808
|
+
write(view, setTableCaption(now, end, value));
|
|
809
|
+
};
|
|
810
|
+
return (_jsxs("label", { className: "edit-table-caption", "data-ms-control": "", children: [_jsx("span", { children: strings.editor.tableCaption }), _jsx("input", { className: "edit-attach-input", value: value, onChange: (event) => setValue(event.target.value), onBlur: commit, onKeyDown: (event) => {
|
|
811
|
+
if (event.key === 'Enter') {
|
|
812
|
+
event.preventDefault();
|
|
813
|
+
commit();
|
|
814
|
+
}
|
|
815
|
+
} })] }));
|
|
772
816
|
}
|
|
773
817
|
/** Shows a block as its text: the cursor goes into it, and the editor draws its source. */
|
|
774
818
|
export function reveal(view, at) {
|
|
@@ -1012,6 +1056,8 @@ export const ICONS = {
|
|
|
1012
1056
|
diagram: icon('M5 3h4v4H5z', 'M15 17h4v4h-4z', 'M7 7v4a2 2 0 0 0 2 2h6a2 2 0 0 1 2 2v2'),
|
|
1013
1057
|
rule: icon('M5 12h14'),
|
|
1014
1058
|
style: icon('M4 7V4h16v3', 'M9 20h6', 'M12 4v16'),
|
|
1059
|
+
propertiesBlock: icon('M3 5h18', 'M3 12h6', 'M13 12h8', 'M3 19h6', 'M13 19h8'),
|
|
1060
|
+
contents: icon('M21 12h-8', 'M21 6H8', 'M21 18h-8', 'M3 6v4c0 1.1.9 2 2 2h3', 'M3 10v6c0 1.1.9 2 2 2h3'),
|
|
1015
1061
|
footnote: icon('M4 7V4h16v3', 'M9 20h6', 'M12 4v16', 'M19 13v6', 'M17 15l2-2'),
|
|
1016
1062
|
comment: icon('M7.9 20A9 9 0 1 0 4 16.1L2 22Z'),
|
|
1017
1063
|
caret: icon('m6 9 6 6 6-6'),
|
|
@@ -1027,3 +1073,54 @@ export const ICONS = {
|
|
|
1027
1073
|
edit: icon('M21.17 6.81a1 1 0 0 0-3.99-3.99L3.84 16.17a2 2 0 0 0-.5.83l-1.32 4.35a.5.5 0 0 0 .62.62l4.35-1.32a2 2 0 0 0 .83-.5z', 'm15 5 4 4'),
|
|
1028
1074
|
view: icon('M2.06 12.35a1 1 0 0 1 0-.7 10.75 10.75 0 0 1 19.88 0 1 1 0 0 1 0 .7 10.75 10.75 0 0 1-19.88 0', 'M12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z'),
|
|
1029
1075
|
};
|
|
1076
|
+
/**
|
|
1077
|
+
* A `contents` block in edit mode (`SPEC.md` §7.17, #173), while the author is elsewhere: the
|
|
1078
|
+
* contents it draws on the screen, over the note as it is now, or, where it draws nothing here —
|
|
1079
|
+
* moved to the start or the end, or for print alone — a line saying where it is drawn.
|
|
1080
|
+
*/
|
|
1081
|
+
export function ContentsPreview({ body, ordinal }) {
|
|
1082
|
+
const host = useHost();
|
|
1083
|
+
const file = embedsFile(host);
|
|
1084
|
+
const drawn = resolveWikilinks(hideComments(body).text, (name) => wikilinkAddress(host, name), file);
|
|
1085
|
+
const outline = useMemo(() => planOutline([drawn], undefined, host.queryTable !== null), [drawn, host.queryTable]);
|
|
1086
|
+
const blocks = useMemo(() => outline.blocks.map((source) => {
|
|
1087
|
+
const { settings } = readContents(source);
|
|
1088
|
+
return { screen: resolveBlock(settings, host.contents, 'screen'), print: resolveBlock(settings, host.contents, 'print') };
|
|
1089
|
+
}), [outline, host.contents]);
|
|
1090
|
+
const names = useMemo(() => [...new Set(outline.kept.map((reference) => reference.name))].filter((name) => host.keeps(name)), [outline, host.keeps]);
|
|
1091
|
+
const files = useKeptFiles(host, names);
|
|
1092
|
+
const items = useMemo(() => planItems(outline, (name, form) => {
|
|
1093
|
+
if (!host.fileOf)
|
|
1094
|
+
return form === 'image' ? 'image' : 'other';
|
|
1095
|
+
const file = files.get(name);
|
|
1096
|
+
return file === undefined ? 'pending' : file?.mimeType.startsWith('image/') ? 'image' : 'other';
|
|
1097
|
+
}, host.keeps), [outline, files, host.fileOf, host.keeps]);
|
|
1098
|
+
const mine = blocks[ordinal];
|
|
1099
|
+
if (!mine)
|
|
1100
|
+
return null;
|
|
1101
|
+
if (mine.screen && mine.screen.place === null) {
|
|
1102
|
+
return (_jsx(ContentsContext.Provider, { value: { kind: 'note', headings: outline.headings, items, files, blocks, listed: listedIn(blocks), printing: false, appendices: false }, children: _jsx(ContentsAt, { plans: { screen: mine.screen, print: null }, place: null }) }));
|
|
1103
|
+
}
|
|
1104
|
+
const where = (plan) => (plan === null ? null : (plan.place ?? 'here'));
|
|
1105
|
+
return _jsx("p", { className: "contents-elsewhere", children: host.strings.editor.contentsElsewhere({ screen: where(mine.screen), print: where(mine.print) }) });
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* A `properties` block in edit mode (`SPEC.md` §7.18, #172), while the author is elsewhere: the
|
|
1109
|
+
* entries it draws on the screen, from the note's frontmatter as it is now, or, where it draws
|
|
1110
|
+
* nothing here, a line saying where it is drawn.
|
|
1111
|
+
*/
|
|
1112
|
+
export function PropertiesPreview({ text, ordinal }) {
|
|
1113
|
+
const host = useHost();
|
|
1114
|
+
const properties = useMemo(() => readProperties(text), [text]);
|
|
1115
|
+
const outline = useMemo(() => planOutline([text.slice(bodyStart(text))]), [text]);
|
|
1116
|
+
const source = outline.propertiesBlocks[ordinal];
|
|
1117
|
+
if (source === undefined)
|
|
1118
|
+
return null;
|
|
1119
|
+
const { settings } = readPropertiesBlock(source);
|
|
1120
|
+
const screen = resolvePropertiesBlock(settings, host.properties, 'screen');
|
|
1121
|
+
const print = resolvePropertiesBlock(settings, host.properties, 'print');
|
|
1122
|
+
if (screen && screen.place === null)
|
|
1123
|
+
return _jsx(Properties, { properties: properties, plan: screen, withName: false });
|
|
1124
|
+
const where = (plan) => (plan === null ? null : (plan.place ?? 'here'));
|
|
1125
|
+
return _jsx("p", { className: "contents-elsewhere", children: host.strings.editor.propertiesElsewhere({ screen: where(screen), print: where(print) }) });
|
|
1126
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `contents` block written from the editor's form (`SPEC.md` §7.17, #173): its settings as the
|
|
3
|
+
* block's lines, the block the cursor is in, and the changes that write it. No React and no DOM.
|
|
4
|
+
*/
|
|
5
|
+
import { type ContentsSettings } from '../contents.js';
|
|
6
|
+
import type { TextChange } from './table.js';
|
|
7
|
+
/** A `contents` block of the note, by offsets: the whole fence, and the lines between its fences. */
|
|
8
|
+
export interface ContentsBlockSpan {
|
|
9
|
+
readonly from: number;
|
|
10
|
+
readonly to: number;
|
|
11
|
+
/** The lines between the fences, empty for an empty block; `null` for a fence never closed. */
|
|
12
|
+
readonly content: {
|
|
13
|
+
readonly from: number;
|
|
14
|
+
readonly to: number;
|
|
15
|
+
} | null;
|
|
16
|
+
readonly settings: ContentsSettings;
|
|
17
|
+
}
|
|
18
|
+
/** Every `contents` block of the note, in order. */
|
|
19
|
+
export declare function contentsBlocks(text: string): ContentsBlockSpan[];
|
|
20
|
+
/** The `contents` block that holds an offset, its fences included, or `null`. */
|
|
21
|
+
export declare function contentsBlockAt(text: string, at: number): ContentsBlockSpan | null;
|
|
22
|
+
/**
|
|
23
|
+
* The lines a block writes for its settings: only what they set, the root first, then `screen`
|
|
24
|
+
* and `print`. Settings that set nothing are an empty block, which is the classic contents.
|
|
25
|
+
*/
|
|
26
|
+
export declare function contentsText(settings: ContentsSettings): string;
|
|
27
|
+
/** Rewrites the lines of a block to `settings`, its fences kept. */
|
|
28
|
+
export declare function setContents(block: ContentsBlockSpan, settings: ContentsSettings): TextChange[];
|
|
29
|
+
/** A new block of these settings, as the editor inserts it on a line of its own. */
|
|
30
|
+
export declare function newContentsBlock(settings: ContentsSettings): string;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `contents` block written from the editor's form (`SPEC.md` §7.17, #173): its settings as the
|
|
3
|
+
* block's lines, the block the cursor is in, and the changes that write it. No React and no DOM.
|
|
4
|
+
*/
|
|
5
|
+
import { CONTENTS_LISTS, CONTENTS_MEDIA, readContents } from '../contents.js';
|
|
6
|
+
import { analyze } from './analyze.js';
|
|
7
|
+
/** Every `contents` block of the note, in order. */
|
|
8
|
+
export function contentsBlocks(text) {
|
|
9
|
+
return analyze(text).constructs.flatMap((construct) => {
|
|
10
|
+
if (construct.kind !== 'code' || construct.language !== 'contents' || !construct.open)
|
|
11
|
+
return [];
|
|
12
|
+
const fence = text.slice(construct.open.from, construct.open.to);
|
|
13
|
+
if (!/^\s*(`{3,}|~{3,})\s*contents\s*$/.test(fence))
|
|
14
|
+
return [];
|
|
15
|
+
const from = construct.open.to + 1;
|
|
16
|
+
const content = construct.close ? { from: Math.min(from, construct.close.from), to: construct.close.from } : null;
|
|
17
|
+
return [{ from: construct.from, to: construct.to, content, settings: readContents(construct.code).settings }];
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** The `contents` block that holds an offset, its fences included, or `null`. */
|
|
21
|
+
export function contentsBlockAt(text, at) {
|
|
22
|
+
return contentsBlocks(text).find((block) => block.from <= at && at <= block.to) ?? null;
|
|
23
|
+
}
|
|
24
|
+
function listLines(options, indent) {
|
|
25
|
+
const lines = [];
|
|
26
|
+
if (options?.title)
|
|
27
|
+
lines.push(`${indent}title: ${options.title}`);
|
|
28
|
+
if (options?.depth !== undefined)
|
|
29
|
+
lines.push(`${indent}depth: ${options.depth}`);
|
|
30
|
+
return lines;
|
|
31
|
+
}
|
|
32
|
+
/** The lines of one layer: each list that sets something, and `place`. */
|
|
33
|
+
function layerLines(layer, indent, all) {
|
|
34
|
+
const lines = [];
|
|
35
|
+
for (const kind of CONTENTS_LISTS) {
|
|
36
|
+
if (!(kind in layer))
|
|
37
|
+
continue;
|
|
38
|
+
const options = listLines(layer[kind], `${indent} `);
|
|
39
|
+
// A list with nothing to say is written only where it chooses the lists, beside others.
|
|
40
|
+
if (options.length > 0 || all || kind !== 'headings')
|
|
41
|
+
lines.push(`${indent}${kind}:`, ...options);
|
|
42
|
+
}
|
|
43
|
+
if (layer.place)
|
|
44
|
+
lines.push(`${indent}place: ${layer.place}`);
|
|
45
|
+
return lines;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The lines a block writes for its settings: only what they set, the root first, then `screen`
|
|
49
|
+
* and `print`. Settings that set nothing are an empty block, which is the classic contents.
|
|
50
|
+
*/
|
|
51
|
+
export function contentsText(settings) {
|
|
52
|
+
const lists = (layer) => CONTENTS_LISTS.filter((kind) => kind in layer).length;
|
|
53
|
+
const lines = layerLines(settings, '', lists(settings) > 1);
|
|
54
|
+
for (const medium of CONTENTS_MEDIA) {
|
|
55
|
+
const layer = settings[medium];
|
|
56
|
+
if (layer === 'none')
|
|
57
|
+
lines.push(`${medium}: none`);
|
|
58
|
+
else if (layer) {
|
|
59
|
+
// A medium that names its lists names each, the headings alone included.
|
|
60
|
+
const inner = layerLines(layer, ' ', true);
|
|
61
|
+
if (inner.length > 0)
|
|
62
|
+
lines.push(`${medium}:`, ...inner);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return lines.map((line) => `${line}\n`).join('');
|
|
66
|
+
}
|
|
67
|
+
/** Rewrites the lines of a block to `settings`, its fences kept. */
|
|
68
|
+
export function setContents(block, settings) {
|
|
69
|
+
if (!block.content)
|
|
70
|
+
return [];
|
|
71
|
+
return [{ from: block.content.from, to: block.content.to, insert: contentsText(settings) }];
|
|
72
|
+
}
|
|
73
|
+
/** A new block of these settings, as the editor inserts it on a line of its own. */
|
|
74
|
+
export function newContentsBlock(settings) {
|
|
75
|
+
return `\`\`\`contents\n${contentsText(settings)}\`\`\``;
|
|
76
|
+
}
|
package/dist/edit/live.js
CHANGED
|
@@ -19,11 +19,13 @@ import { MarkdownImage } from '../Markdown.js';
|
|
|
19
19
|
import { highlightLanguage } from '../highlight.js';
|
|
20
20
|
import { analyze } from './analyze.js';
|
|
21
21
|
import { DEFAULT_METRICS, estimateHeight, lineHeightOf, measureLines, measuredHeight, rememberHeight, withinReason } from './heights.js';
|
|
22
|
-
import { AddProperties, CalloutBlock, ImageBlock, PropertiesBlock, reveal, Snippet, StyleSummary, TableBlock } from './blocks.js';
|
|
22
|
+
import { AddProperties, CalloutBlock, ContentsPreview, PropertiesPreview, ImageBlock, PropertiesBlock, reveal, Snippet, StyleSummary, TableBlock } from './blocks.js';
|
|
23
23
|
import { justifies, styleProperties } from '../note-style.js';
|
|
24
24
|
import { caretAt, sourceOffsetOf } from './click-source.js';
|
|
25
25
|
import { LanguageControl } from './LanguageControl.js';
|
|
26
26
|
import { toggleTaskBox } from './commands.js';
|
|
27
|
+
import { findStyleBlocks, styleAt } from '../style-block.js';
|
|
28
|
+
import { planOutline } from '../outline.js';
|
|
27
29
|
export const editorEnvironment = Facet.define({
|
|
28
30
|
combine: (values) => values[0] ?? { host: resolveHost({}) },
|
|
29
31
|
});
|
|
@@ -359,6 +361,40 @@ class LineEstimate extends WidgetType {
|
|
|
359
361
|
return this.height;
|
|
360
362
|
}
|
|
361
363
|
}
|
|
364
|
+
/** A heading's number, as the style numbers the outline (#174), drawn before its text. */
|
|
365
|
+
class HeadingNumberWidget extends WidgetType {
|
|
366
|
+
number;
|
|
367
|
+
constructor(number) {
|
|
368
|
+
super();
|
|
369
|
+
this.number = number;
|
|
370
|
+
}
|
|
371
|
+
eq(other) {
|
|
372
|
+
return other.number === this.number;
|
|
373
|
+
}
|
|
374
|
+
toDOM() {
|
|
375
|
+
const element = document.createElement('span');
|
|
376
|
+
element.className = 'heading-number cm-ms-heading-number';
|
|
377
|
+
element.textContent = `${this.number} `;
|
|
378
|
+
element.setAttribute('aria-hidden', 'true');
|
|
379
|
+
return element;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* The number of each heading of the body, by its line in the document, where the notebook's style
|
|
384
|
+
* or a style block numbers them (#174); empty where nothing does.
|
|
385
|
+
*/
|
|
386
|
+
function headingNumbers(text, bodyFrom, numbering, firstLine) {
|
|
387
|
+
const numbers = new Map();
|
|
388
|
+
const body = text.slice(bodyFrom);
|
|
389
|
+
if (numbering !== 'decimal' && !/^[ \t]*numbering:/m.test(body))
|
|
390
|
+
return numbers;
|
|
391
|
+
const found = findStyleBlocks(body);
|
|
392
|
+
const outline = planOutline([found.text], (_run, line) => styleAt(found.blocks, line).numbering ?? numbering ?? 'none');
|
|
393
|
+
for (const heading of outline.headings)
|
|
394
|
+
if (heading.number !== null)
|
|
395
|
+
numbers.set(firstLine + heading.line - 1, heading.number);
|
|
396
|
+
return numbers;
|
|
397
|
+
}
|
|
362
398
|
class BulletWidget extends WidgetType {
|
|
363
399
|
eq() {
|
|
364
400
|
return true;
|
|
@@ -530,6 +566,14 @@ function build(state) {
|
|
|
530
566
|
styleOfLine.set(at, { properties, lang: justifies(style) ? host.strings.locale : null, css: Object.entries(properties).map(([name, value]) => `${name}:${value}`).join(';') });
|
|
531
567
|
}
|
|
532
568
|
}
|
|
569
|
+
// The `contents` blocks met so far, and what their drawing depends on besides their lines: the
|
|
570
|
+
// note's headings (#173).
|
|
571
|
+
let contentsSeen = 0;
|
|
572
|
+
let propertiesSeen = 0;
|
|
573
|
+
/** The number of each heading, by its line, read the first time a heading needs it (#174). */
|
|
574
|
+
let numbers = null;
|
|
575
|
+
let signature = null;
|
|
576
|
+
const headingsSignature = () => (signature ??= constructs.flatMap((construct) => (construct.kind === 'heading' ? [text.slice(construct.from, construct.to)] : [])).join('\n'));
|
|
533
577
|
const block = (from, to, kind, key, draw, revealsOnClick = true) => {
|
|
534
578
|
const lines = linesOf(from, to);
|
|
535
579
|
const source = text.slice(lines.from, lines.to);
|
|
@@ -647,6 +691,26 @@ function build(state) {
|
|
|
647
691
|
block(from, to, 'diagram', `table-block:${source}`, () => createElement(Snippet, { text: source }));
|
|
648
692
|
break;
|
|
649
693
|
}
|
|
694
|
+
// A `contents` block is drawn as the contents it draws on the screen, over the note as it is
|
|
695
|
+
// now, while the author is elsewhere, and edited as its lines (§7.17, #173).
|
|
696
|
+
// A `properties` block is drawn as the entries it draws on the screen while the author is
|
|
697
|
+
// elsewhere, and edited as its lines (§7.18, #172).
|
|
698
|
+
if (/^\s*(`{3,}|~{3,})\s*properties\s*$/.test(fence)) {
|
|
699
|
+
const ordinal = propertiesSeen++;
|
|
700
|
+
if (!open && alone(from, to)) {
|
|
701
|
+
const frontmatter = bodyFrom > 0 ? text.slice(0, bodyFrom) : '';
|
|
702
|
+
block(from, to, 'diagram', `properties:${ordinal}:${source}:${frontmatter}`, () => createElement(PropertiesPreview, { text, ordinal }));
|
|
703
|
+
break;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (/^\s*(`{3,}|~{3,})\s*contents\s*$/.test(fence)) {
|
|
707
|
+
const ordinal = contentsSeen++;
|
|
708
|
+
if (!open && alone(from, to)) {
|
|
709
|
+
const body = text.slice(bodyFrom);
|
|
710
|
+
block(from, to, 'diagram', `contents:${ordinal}:${source}:${headingsSignature()}`, () => createElement(ContentsPreview, { body, ordinal }));
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
650
714
|
codeLines(construct, out, doc, text);
|
|
651
715
|
// The language, chosen at the block's top right from every grammar Prism carries (#84).
|
|
652
716
|
if (construct.open) {
|
|
@@ -677,8 +741,14 @@ function build(state) {
|
|
|
677
741
|
headingLevels.set(doc.lineAt(from).number, construct.level);
|
|
678
742
|
if (touches(doc.lineAt(from).from, doc.lineAt(from).to))
|
|
679
743
|
out.push(syntax.range(marker.from, marker.to));
|
|
680
|
-
else
|
|
744
|
+
else {
|
|
681
745
|
out.push(hide.range(marker.from, marker.to));
|
|
746
|
+
// Its number, as reading draws it, while the author is elsewhere (#174).
|
|
747
|
+
numbers ??= headingNumbers(text, bodyFrom, host.noteStyle.numbering, doc.lineAt(bodyFrom).number);
|
|
748
|
+
const number = numbers.get(doc.lineAt(from).number);
|
|
749
|
+
if (number)
|
|
750
|
+
out.push(Decoration.widget({ widget: new HeadingNumberWidget(number), side: 1 }).range(marker.to));
|
|
751
|
+
}
|
|
682
752
|
break;
|
|
683
753
|
}
|
|
684
754
|
case 'mark': {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `properties` block written from the editor's form (`SPEC.md` §7.18, #172): its settings as the
|
|
3
|
+
* block's lines, and the block the cursor is in. No React and no DOM.
|
|
4
|
+
*/
|
|
5
|
+
import { type PropertiesSettings } from '../properties-block.js';
|
|
6
|
+
import type { TextChange } from './table.js';
|
|
7
|
+
/** A `properties` block of the note, by offsets, with the lines between its fences. */
|
|
8
|
+
export interface PropertiesBlockSpan {
|
|
9
|
+
readonly from: number;
|
|
10
|
+
readonly to: number;
|
|
11
|
+
readonly content: {
|
|
12
|
+
readonly from: number;
|
|
13
|
+
readonly to: number;
|
|
14
|
+
} | null;
|
|
15
|
+
readonly settings: PropertiesSettings;
|
|
16
|
+
}
|
|
17
|
+
/** Every `properties` block of the note, in order. */
|
|
18
|
+
export declare function propertiesBlocks(text: string): PropertiesBlockSpan[];
|
|
19
|
+
/** The `properties` block that holds an offset, its fences included, or `null`. */
|
|
20
|
+
export declare function propertiesBlockAt(text: string, at: number): PropertiesBlockSpan | null;
|
|
21
|
+
/** The lines a block writes for its settings: only what they set, the root first, then the media. */
|
|
22
|
+
export declare function propertiesText(settings: PropertiesSettings): string;
|
|
23
|
+
/** Rewrites the lines of a block to `settings`, its fences kept. */
|
|
24
|
+
export declare function setProperties(block: PropertiesBlockSpan, settings: PropertiesSettings): TextChange[];
|
|
25
|
+
/** A new block of these settings, as the editor inserts it on a line of its own. */
|
|
26
|
+
export declare function newPropertiesBlock(settings: PropertiesSettings): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `properties` block written from the editor's form (`SPEC.md` §7.18, #172): its settings as the
|
|
3
|
+
* block's lines, and the block the cursor is in. No React and no DOM.
|
|
4
|
+
*/
|
|
5
|
+
import { CONTENTS_MEDIA } from '../contents.js';
|
|
6
|
+
import { readPropertiesBlock } from '../properties-block.js';
|
|
7
|
+
import { analyze } from './analyze.js';
|
|
8
|
+
/** Every `properties` block of the note, in order. */
|
|
9
|
+
export function propertiesBlocks(text) {
|
|
10
|
+
return analyze(text).constructs.flatMap((construct) => {
|
|
11
|
+
if (construct.kind !== 'code' || construct.language !== 'properties' || !construct.open)
|
|
12
|
+
return [];
|
|
13
|
+
if (!/^\s*(`{3,}|~{3,})\s*properties\s*$/.test(text.slice(construct.open.from, construct.open.to)))
|
|
14
|
+
return [];
|
|
15
|
+
const from = construct.open.to + 1;
|
|
16
|
+
const content = construct.close ? { from: Math.min(from, construct.close.from), to: construct.close.from } : null;
|
|
17
|
+
return [{ from: construct.from, to: construct.to, content, settings: readPropertiesBlock(construct.code).settings }];
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** The `properties` block that holds an offset, its fences included, or `null`. */
|
|
21
|
+
export function propertiesBlockAt(text, at) {
|
|
22
|
+
return propertiesBlocks(text).find((block) => block.from <= at && at <= block.to) ?? null;
|
|
23
|
+
}
|
|
24
|
+
function layerLines(layer, indent) {
|
|
25
|
+
const lines = [];
|
|
26
|
+
if (layer.keys && layer.keys.length > 0)
|
|
27
|
+
lines.push(`${indent}keys: ${layer.keys.join(', ')}`);
|
|
28
|
+
if (layer.title)
|
|
29
|
+
lines.push(`${indent}title: ${layer.title}`);
|
|
30
|
+
if (layer.fold)
|
|
31
|
+
lines.push(`${indent}fold: ${layer.fold}`);
|
|
32
|
+
if (layer.place)
|
|
33
|
+
lines.push(`${indent}place: ${layer.place}`);
|
|
34
|
+
return lines;
|
|
35
|
+
}
|
|
36
|
+
/** The lines a block writes for its settings: only what they set, the root first, then the media. */
|
|
37
|
+
export function propertiesText(settings) {
|
|
38
|
+
const lines = layerLines(settings, '');
|
|
39
|
+
for (const medium of CONTENTS_MEDIA) {
|
|
40
|
+
const layer = settings[medium];
|
|
41
|
+
if (layer === 'none')
|
|
42
|
+
lines.push(`${medium}: none`);
|
|
43
|
+
else if (layer) {
|
|
44
|
+
const inner = layerLines(layer, ' ');
|
|
45
|
+
if (inner.length > 0)
|
|
46
|
+
lines.push(`${medium}:`, ...inner);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return lines.map((line) => `${line}\n`).join('');
|
|
50
|
+
}
|
|
51
|
+
/** Rewrites the lines of a block to `settings`, its fences kept. */
|
|
52
|
+
export function setProperties(block, settings) {
|
|
53
|
+
if (!block.content)
|
|
54
|
+
return [];
|
|
55
|
+
return [{ from: block.content.from, to: block.content.to, insert: propertiesText(settings) }];
|
|
56
|
+
}
|
|
57
|
+
/** A new block of these settings, as the editor inserts it on a line of its own. */
|
|
58
|
+
export function newPropertiesBlock(settings) {
|
|
59
|
+
return `\`\`\`properties\n${propertiesText(settings)}\`\`\``;
|
|
60
|
+
}
|