@memorysmithapp/markdown-renderer 0.11.2 → 0.12.1
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 +3 -1
- package/dist/DataTable.js +118 -3
- package/dist/Markdown.d.ts +17 -1
- package/dist/Markdown.js +21 -4
- package/dist/NoteBody.js +48 -5
- package/dist/appendices.d.ts +51 -0
- package/dist/appendices.js +102 -0
- package/dist/diagnose.d.ts +2 -1
- package/dist/diagnose.js +10 -1
- package/dist/edit/NoteEditor.js +36 -5
- package/dist/edit/StyleDialog.d.ts +16 -0
- package/dist/edit/StyleDialog.js +64 -0
- package/dist/edit/analyze.d.ts +20 -1
- package/dist/edit/analyze.js +30 -1
- package/dist/edit/blocks.d.ts +11 -0
- package/dist/edit/blocks.js +24 -2
- package/dist/edit/heights.d.ts +1 -1
- package/dist/edit/heights.js +3 -0
- package/dist/edit/live.js +68 -2
- package/dist/edit/style.d.ts +29 -0
- package/dist/edit/style.js +71 -0
- package/dist/edit/table.d.ts +1 -1
- package/dist/edit/table.js +4 -1
- package/dist/host.d.ts +31 -1
- package/dist/host.js +7 -0
- package/dist/index.d.ts +3 -1
- package/dist/justified.d.ts +25 -0
- package/dist/justified.js +96 -0
- package/dist/note-style.d.ts +38 -0
- package/dist/note-style.js +53 -0
- package/dist/source.js +3 -2
- package/dist/strings.d.ts +36 -5
- package/dist/strings.js +20 -4
- package/dist/style-block.d.ts +78 -0
- package/dist/style-block.js +178 -0
- package/dist/table-block.d.ts +14 -0
- package/dist/theme.d.ts +12 -1
- package/dist/theme.js +8 -6
- package/package.json +2 -2
- package/structure.css +107 -0
- package/theme-default.css +132 -14
package/dist/edit/NoteEditor.js
CHANGED
|
@@ -15,6 +15,7 @@ import { Annotation, Compartment, EditorState } from '@codemirror/state';
|
|
|
15
15
|
import { EditorView, keymap } from '@codemirror/view';
|
|
16
16
|
import { useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState } from 'react';
|
|
17
17
|
import { HOST_KEYS, HostProvider, isAttachRefusal, resolveHost, useDrawing } from '../host.js';
|
|
18
|
+
import { justifies } from '../note-style.js';
|
|
18
19
|
import { rootStyle } from '../theme.js';
|
|
19
20
|
import { ICONS } from './blocks.js';
|
|
20
21
|
import { BLOCKS, command, headingLevel, insertBlock, insertFootnote, insertLink, insertWikilink, setHeading, toggleComment, toggleList, toggleMark, toggleQuote, } from './commands.js';
|
|
@@ -22,6 +23,8 @@ import { analysisField, editorEnvironment, livePage } from './live.js';
|
|
|
22
23
|
import { measureMetrics } from './heights.js';
|
|
23
24
|
import { NoteActions } from '../NoteActions.js';
|
|
24
25
|
import { AttachDialog, webImage } from './AttachDialog.js';
|
|
26
|
+
import { StyleDialog } from './StyleDialog.js';
|
|
27
|
+
import { setStyleOptions, styleBlockAt, wrapInStyle } from './style.js';
|
|
25
28
|
import { ContextMenu } from './ContextMenu.js';
|
|
26
29
|
import { chooseLanguageAt } from './LanguageControl.js';
|
|
27
30
|
import { useEditSaves } from './saves.js';
|
|
@@ -132,14 +135,14 @@ function attachSays(attaching, words) {
|
|
|
132
135
|
return attaching.queued > 0 ? `${step} (${words.attachQueued(attaching.queued)})` : step;
|
|
133
136
|
}
|
|
134
137
|
/** The toolbar of the editor: the marks, the blocks and the history, each a command. */
|
|
135
|
-
function Toolbar({ view, host, end, opened, onAttach, attaching, attachButton, }) {
|
|
138
|
+
function Toolbar({ view, host, end, opened, onAttach, onStyle, attaching, attachButton, }) {
|
|
136
139
|
const words = host.strings.editor;
|
|
137
140
|
const disabled = !view || view.state.readOnly;
|
|
138
141
|
const run = (action) => () => view && action(view);
|
|
139
142
|
const button = (name, action, shortcut) => (_jsx("button", { type: "button", className: "edit-control", title: shortcut ? `${words[name]} (${shortcut})` : words[name], "aria-label": words[name], disabled: disabled, onMouseDown: (event) => event.preventDefault(), onClick: run(action), children: ICONS[name] }, name));
|
|
140
143
|
const block = (name) => button(name, insertBlockCommand(name));
|
|
141
144
|
const level = view ? headingLevel(view.state) : 0;
|
|
142
|
-
return (_jsxs("div", { className: "edit-toolbar", role: "toolbar", "aria-label": words.toolbar, children: [_jsxs("div", { className: "edit-toolbar-tools", children: [_jsxs("span", { className: "edit-toolbar-group", children: [_jsx("button", { type: "button", className: "edit-control", title: words.revert, "aria-label": words.revert, disabled: !view || view.state.readOnly || view.state.doc.toString() === opened, onMouseDown: (event) => event.preventDefault(), onClick: run(command((state) => revert(state, opened))), children: ICONS.revert }), button('undo', undo, 'Mod-Z'), button('redo', redo, 'Mod-Shift-Z, Mod-Y')] }), _jsx("span", { className: "edit-toolbar-group", children: _jsxs("select", { className: "edit-heading", "aria-label": words.heading, title: words.heading, value: level, disabled: disabled, onChange: (event) => view && command((state) => setHeading(state, Number(event.target.value)))(view), children: [_jsx("option", { value: 0, children: words.paragraph }), [1, 2, 3, 4, 5, 6].map((each) => (_jsx("option", { value: each, children: `${words.heading} ${each}` }, each)))] }) }), _jsxs("span", { className: "edit-toolbar-group", children: [button('bold', command((state) => toggleMark(state, '**')), 'Mod-B'), button('italic', command((state) => toggleMark(state, '_')), 'Mod-I'), button('strikethrough', command((state) => toggleMark(state, '~~')), 'Mod-Shift-X'), button('highlight', command((state) => toggleMark(state, '==')), 'Mod-Shift-H'), button('code', command((state) => toggleMark(state, '`')), 'Mod-E')] }), _jsxs("span", { className: "edit-toolbar-group", children: [button('link', command(insertLink), 'Mod-K'), button('wikilink', command(insertWikilink), 'Mod-Shift-K'), button('footnote', command(insertFootnote)), button('comment', command(toggleComment), 'Mod-/'), _jsxs("span", { className: "edit-attach-control", children: [_jsx("button", { ref: attachButton, type: "button", className: "edit-control", title: attachSays(attaching, words), "aria-label": words.attach, "aria-busy": attaching.state === 'busy' ? true : undefined, "data-attach": attaching.state, disabled: disabled, onMouseDown: (event) => event.preventDefault(), onClick: onAttach, children: attaching.state === 'busy' ? ICONS.saving : attaching.state === 'failed' ? ICONS.notSaved : ICONS.attach }), attaching.state === 'busy' && attaching.fraction !== null && (_jsx("span", { className: "edit-attach-progress", role: "progressbar", "aria-label": words.attach, "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": Math.round(attaching.fraction * 100), style: { ['--edit-attach-fraction']: attaching.fraction } })), _jsx("span", { className: "visually-hidden", role: "status", "aria-live": "polite", children: attaching.state === 'idle' ? '' : attachSays(attaching, words) })] })] }), _jsxs("span", { className: "edit-toolbar-group", children: [button('bulletList', command((state) => toggleList(state, 'bullet')), 'Mod-Shift-8'), button('orderedList', command((state) => toggleList(state, 'ordered')), 'Mod-Shift-7'), button('taskList', command((state) => toggleList(state, 'task')), 'Mod-Shift-9'), button('quote', command(toggleQuote), 'Mod-Shift-.')] }), _jsxs("span", { className: "edit-toolbar-group", children: [block('callout'), block('table'), block('codeBlock'), block('math'), block('diagram'), block('rule')] })] }), end] }));
|
|
145
|
+
return (_jsxs("div", { className: "edit-toolbar", role: "toolbar", "aria-label": words.toolbar, children: [_jsxs("div", { className: "edit-toolbar-tools", children: [_jsxs("span", { className: "edit-toolbar-group", children: [_jsx("button", { type: "button", className: "edit-control", title: words.revert, "aria-label": words.revert, disabled: !view || view.state.readOnly || view.state.doc.toString() === opened, onMouseDown: (event) => event.preventDefault(), onClick: run(command((state) => revert(state, opened))), children: ICONS.revert }), button('undo', undo, 'Mod-Z'), button('redo', redo, 'Mod-Shift-Z, Mod-Y')] }), _jsx("span", { className: "edit-toolbar-group", children: _jsxs("select", { className: "edit-heading", "aria-label": words.heading, title: words.heading, value: level, disabled: disabled, onChange: (event) => view && command((state) => setHeading(state, Number(event.target.value)))(view), children: [_jsx("option", { value: 0, children: words.paragraph }), [1, 2, 3, 4, 5, 6].map((each) => (_jsx("option", { value: each, children: `${words.heading} ${each}` }, each)))] }) }), _jsxs("span", { className: "edit-toolbar-group", children: [button('bold', command((state) => toggleMark(state, '**')), 'Mod-B'), button('italic', command((state) => toggleMark(state, '_')), 'Mod-I'), button('strikethrough', command((state) => toggleMark(state, '~~')), 'Mod-Shift-X'), button('highlight', command((state) => toggleMark(state, '==')), 'Mod-Shift-H'), button('code', command((state) => toggleMark(state, '`')), 'Mod-E')] }), _jsxs("span", { className: "edit-toolbar-group", children: [button('link', command(insertLink), 'Mod-K'), button('wikilink', command(insertWikilink), 'Mod-Shift-K'), button('footnote', command(insertFootnote)), button('comment', command(toggleComment), 'Mod-/'), _jsxs("span", { className: "edit-attach-control", children: [_jsx("button", { ref: attachButton, type: "button", className: "edit-control", title: attachSays(attaching, words), "aria-label": words.attach, "aria-busy": attaching.state === 'busy' ? true : undefined, "data-attach": attaching.state, disabled: disabled, onMouseDown: (event) => event.preventDefault(), onClick: onAttach, children: attaching.state === 'busy' ? ICONS.saving : attaching.state === 'failed' ? ICONS.notSaved : ICONS.attach }), attaching.state === 'busy' && attaching.fraction !== null && (_jsx("span", { className: "edit-attach-progress", role: "progressbar", "aria-label": words.attach, "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": Math.round(attaching.fraction * 100), style: { ['--edit-attach-fraction']: attaching.fraction } })), _jsx("span", { className: "visually-hidden", role: "status", "aria-live": "polite", children: attaching.state === 'idle' ? '' : attachSays(attaching, words) })] })] }), _jsxs("span", { className: "edit-toolbar-group", children: [button('bulletList', command((state) => toggleList(state, 'bullet')), 'Mod-Shift-8'), button('orderedList', command((state) => toggleList(state, 'ordered')), 'Mod-Shift-7'), button('taskList', command((state) => toggleList(state, 'task')), 'Mod-Shift-9'), button('quote', command(toggleQuote), 'Mod-Shift-.')] }), _jsxs("span", { className: "edit-toolbar-group", children: [block('callout'), block('table'), block('codeBlock'), block('math'), block('diagram'), block('rule'), _jsx("button", { type: "button", className: "edit-control", title: words.style, "aria-label": words.style, "aria-haspopup": "dialog", disabled: disabled, onMouseDown: (event) => event.preventDefault(), onClick: onStyle, children: ICONS.style })] })] }), end] }));
|
|
143
146
|
}
|
|
144
147
|
/**
|
|
145
148
|
* Where the save of the author's last change stands, in the toolbar before the switch: an icon for
|
|
@@ -267,6 +270,31 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
|
|
|
267
270
|
};
|
|
268
271
|
const attachButton = useRef(null);
|
|
269
272
|
const [attachOpen, setAttachOpen] = useState(false);
|
|
273
|
+
/** The form of a style block, open over what it sets now and whether it changes a block (#166). */
|
|
274
|
+
const [styling, setStyling] = useState(null);
|
|
275
|
+
const openStyle = () => {
|
|
276
|
+
const current = editorView.current;
|
|
277
|
+
if (!current)
|
|
278
|
+
return;
|
|
279
|
+
const block = styleBlockAt(current.state.doc.toString(), current.state.selection.main.head);
|
|
280
|
+
setStyling({ initial: block?.style ?? {}, editing: block !== null });
|
|
281
|
+
};
|
|
282
|
+
/** Writes the form's style: the options of the block the cursor is in, or a new block around the passage. */
|
|
283
|
+
const applyStyle = (style) => {
|
|
284
|
+
const current = editorView.current;
|
|
285
|
+
if (!current)
|
|
286
|
+
return;
|
|
287
|
+
const text = current.state.doc.toString();
|
|
288
|
+
const selection = current.state.selection.main;
|
|
289
|
+
const block = styleBlockAt(text, selection.head);
|
|
290
|
+
if (block) {
|
|
291
|
+
current.dispatch({ changes: setStyleOptions(block, style), userEvent: 'input.style', scrollIntoView: true });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
// The cursor at the start of the content: past the opening fence and options, which come first.
|
|
295
|
+
const wrapped = wrapInStyle(text, selection.from, selection.to, style);
|
|
296
|
+
current.dispatch({ changes: wrapped.changes, selection: { anchor: wrapped.cursor }, userEvent: 'input.style', scrollIntoView: true });
|
|
297
|
+
};
|
|
270
298
|
/** Where the editing menu is open, in the window's coordinates (#81). */
|
|
271
299
|
const [menu, setMenu] = useState(null);
|
|
272
300
|
const openMenu = useRef((x, y) => setMenu({ x, y }));
|
|
@@ -312,6 +340,7 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
|
|
|
312
340
|
block('math'),
|
|
313
341
|
block('diagram'),
|
|
314
342
|
block('rule'),
|
|
343
|
+
{ name: 'style', label: words.style, icon: ICONS.style, run: openStyle },
|
|
315
344
|
item('footnote', command(insertFootnote)),
|
|
316
345
|
item('comment', command(toggleComment), 'Mod-/'),
|
|
317
346
|
{ name: 'attach', label: words.attach, icon: ICONS.attach, run: () => setAttachOpen(true) },
|
|
@@ -536,7 +565,9 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
|
|
|
536
565
|
clearTimeout(timer);
|
|
537
566
|
};
|
|
538
567
|
}, [leaving, saves.state]);
|
|
539
|
-
return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name} markdown-editing`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme
|
|
568
|
+
return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name} markdown-editing`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme, resolved.noteStyle),
|
|
569
|
+
// Justified text hyphenates in the reader's language, which the browser has to be told (#164).
|
|
570
|
+
lang: justifies(resolved.noteStyle) ? resolved.strings.locale : undefined, children: [_jsx("div", { ref: bar, className: `edit-bar${buttons === 'responsive' ? ' has-responsive-buttons' : ''}`, "data-leaving": folding ? '' : undefined, children: _jsx(Toolbar, { view: view, host: resolved, opened: opened.current, onAttach: () => {
|
|
540
571
|
// Opening the paperclip is the author acting on a refusal: the dialog says it once more.
|
|
541
572
|
if (attaching.state === 'failed') {
|
|
542
573
|
setRefusal(attaching.message);
|
|
@@ -545,9 +576,9 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
|
|
|
545
576
|
else
|
|
546
577
|
setRefusal(null);
|
|
547
578
|
setAttachOpen(true);
|
|
548
|
-
}, attaching: attaching, attachButton: attachButton, end: _jsxs("div", { className: "edit-corner", "aria-busy": leaving && saves.state === 'saving' ? true : undefined, "data-blocked": blocked ? '' : undefined, children: [_jsx(SaveStatus, { saves: saves, blocked: blocked, deciding: deciding, open: noticeOpen, setOpen: setNoticeOpen, host: resolved, onDecided: () => view?.focus() }), corner ?? _jsx(NoteActions, { display: buttons, text: () => (view ? output(view.state) : given) })] }, blocked ? `blocked-${refusals}` : 'corner') }) }), menu && view && (_jsx(ContextMenu, { x: menu.x, y: menu.y, label: resolved.strings.editor.menu, groups: menuGroups(view), onClose: (back) => {
|
|
579
|
+
}, onStyle: openStyle, attaching: attaching, attachButton: attachButton, end: _jsxs("div", { className: "edit-corner", "aria-busy": leaving && saves.state === 'saving' ? true : undefined, "data-blocked": blocked ? '' : undefined, children: [_jsx(SaveStatus, { saves: saves, blocked: blocked, deciding: deciding, open: noticeOpen, setOpen: setNoticeOpen, host: resolved, onDecided: () => view?.focus() }), corner ?? _jsx(NoteActions, { display: buttons, text: () => (view ? output(view.state) : given) })] }, blocked ? `blocked-${refusals}` : 'corner') }) }), menu && view && (_jsx(ContextMenu, { x: menu.x, y: menu.y, label: resolved.strings.editor.menu, groups: menuGroups(view), onClose: (back) => {
|
|
549
580
|
setMenu(null);
|
|
550
581
|
if (back)
|
|
551
582
|
view.focus();
|
|
552
|
-
} })), attachOpen && (_jsx(AttachDialog, { refusal: refusal, onClose: () => setAttachOpen(false), onClosed: (inserted) => !inserted && attachButton.current?.focus(), onAttach: (files) => attachFiles.current(files), onEmbed: (name) => insertLine(`![[${name}]]`), onLink: (name, address) => insertLine(webImage(name, address)) })), lineBreak === null && _jsx("p", { className: "edit-unavailable", children: resolved.strings.editUnavailable }), _jsx("div", { ref: parent, className: "markdown-editor" })] }) }));
|
|
583
|
+
} })), attachOpen && (_jsx(AttachDialog, { refusal: refusal, onClose: () => setAttachOpen(false), onClosed: (inserted) => !inserted && attachButton.current?.focus(), onAttach: (files) => attachFiles.current(files), onEmbed: (name) => insertLine(`![[${name}]]`), onLink: (name, address) => insertLine(webImage(name, address)) })), styling && (_jsx(StyleDialog, { initial: styling.initial, editing: styling.editing, onApply: applyStyle, onClose: () => setStyling(null), onClosed: () => editorView.current?.focus() })), lineBreak === null && _jsx("p", { className: "edit-unavailable", children: resolved.strings.editUnavailable }), _jsx("div", { ref: parent, className: "markdown-editor" })] }) }));
|
|
553
584
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The form of a `:::{style}` block (`SPEC.md` §7.16, #166): the face and the alignment of headings,
|
|
3
|
+
* text and tables, each starting at the notebook's, which writes no key. It wraps the passage in a
|
|
4
|
+
* new block, or changes the options of the block the cursor is in; the editor writes the bytes.
|
|
5
|
+
*/
|
|
6
|
+
import { type NoteStyle } from '../note-style.js';
|
|
7
|
+
export declare function StyleDialog({ initial, editing, onApply, onClose, onClosed, }: {
|
|
8
|
+
/** What the block sets now, or nothing for a new one. */
|
|
9
|
+
initial: NoteStyle;
|
|
10
|
+
/** Whether it changes a block the cursor is in, rather than wrapping a passage in a new one. */
|
|
11
|
+
editing: boolean;
|
|
12
|
+
onApply: (style: NoteStyle) => void;
|
|
13
|
+
onClose: () => void;
|
|
14
|
+
/** Once the dialog is closed: whether the note was changed, which then has the focus. */
|
|
15
|
+
onClosed: (applied: boolean) => void;
|
|
16
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The form of a `:::{style}` block (`SPEC.md` §7.16, #166): the face and the alignment of headings,
|
|
4
|
+
* text and tables, each starting at the notebook's, which writes no key. It wraps the passage in a
|
|
5
|
+
* new block, or changes the options of the block the cursor is in; the editor writes the bytes.
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useId, useRef, useState } from 'react';
|
|
8
|
+
import { useHost } from '../host.js';
|
|
9
|
+
import { STYLE_ALIGNMENTS, STYLE_FONTS, STYLE_KEYS } from '../note-style.js';
|
|
10
|
+
import { ICONS } from './blocks.js';
|
|
11
|
+
export function StyleDialog({ initial, editing, onApply, onClose, onClosed, }) {
|
|
12
|
+
const host = useHost();
|
|
13
|
+
const words = host.strings.editor;
|
|
14
|
+
const [style, setStyle] = useState(initial);
|
|
15
|
+
const dialog = useRef(null);
|
|
16
|
+
const applied = useRef(false);
|
|
17
|
+
const closed = useRef(onClosed);
|
|
18
|
+
closed.current = onClosed;
|
|
19
|
+
const id = useId();
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const element = dialog.current;
|
|
22
|
+
if (!element)
|
|
23
|
+
return;
|
|
24
|
+
if (typeof element.showModal === 'function')
|
|
25
|
+
element.showModal();
|
|
26
|
+
else
|
|
27
|
+
element.setAttribute('open', '');
|
|
28
|
+
element.querySelector('select')?.focus();
|
|
29
|
+
return () => {
|
|
30
|
+
if (element.open && typeof element.close === 'function')
|
|
31
|
+
element.close();
|
|
32
|
+
closed.current(applied.current);
|
|
33
|
+
};
|
|
34
|
+
}, []);
|
|
35
|
+
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables };
|
|
36
|
+
const value = {
|
|
37
|
+
sans: words.styleSans,
|
|
38
|
+
serif: words.styleSerif,
|
|
39
|
+
left: words.styleLeft,
|
|
40
|
+
center: words.styleCenter,
|
|
41
|
+
right: words.styleRight,
|
|
42
|
+
justify: words.styleJustify,
|
|
43
|
+
};
|
|
44
|
+
const submit = (event) => {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
applied.current = true;
|
|
47
|
+
onClose();
|
|
48
|
+
onApply(style);
|
|
49
|
+
};
|
|
50
|
+
return (_jsx("dialog", { ref: dialog, className: "edit-source edit-attach edit-style", "aria-modal": "true", "aria-labelledby": `${id}-title`, onCancel: (event) => {
|
|
51
|
+
event.preventDefault();
|
|
52
|
+
onClose();
|
|
53
|
+
}, 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.style }), _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: [STYLE_KEYS.map((key) => (_jsxs("label", { children: [_jsx("span", { children: label[key] }), _jsxs("select", { className: "edit-attach-input", name: key, value: style[key] ?? '', onChange: (event) => {
|
|
54
|
+
const chosen = event.target.value;
|
|
55
|
+
setStyle((current) => {
|
|
56
|
+
const next = { ...current };
|
|
57
|
+
if (chosen)
|
|
58
|
+
next[key] = chosen;
|
|
59
|
+
else
|
|
60
|
+
delete next[key];
|
|
61
|
+
return next;
|
|
62
|
+
});
|
|
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 }) })] }) })] }) }));
|
|
64
|
+
}
|
package/dist/edit/analyze.d.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* marked text, comments, block identifiers — by the same patterns the reading surface uses. No
|
|
12
12
|
* React and no DOM: the analysis is a pure function of the text, and tested as one.
|
|
13
13
|
*/
|
|
14
|
+
import { type StyleBlocks } from '../style-block.js';
|
|
15
|
+
import type { NoteStyle } from '../note-style.js';
|
|
14
16
|
/** A range of the text: `from` included, `to` excluded. */
|
|
15
17
|
export interface Span {
|
|
16
18
|
readonly from: number;
|
|
@@ -143,7 +145,24 @@ export type Construct = {
|
|
|
143
145
|
readonly kind: 'definition';
|
|
144
146
|
readonly from: number;
|
|
145
147
|
readonly to: number;
|
|
146
|
-
}
|
|
148
|
+
} | ({
|
|
149
|
+
readonly kind: 'styleBlock';
|
|
150
|
+
} & StyleBlockSpan);
|
|
151
|
+
/** A style block of the note (`SPEC.md` §7.16, #166), by the offsets of its parts. */
|
|
152
|
+
export interface StyleBlockSpan extends Span {
|
|
153
|
+
/** The opening fence's line, without its line break. */
|
|
154
|
+
readonly open: Span;
|
|
155
|
+
/** The options, from the first `---` to the end of the last, or `null` when it has none. */
|
|
156
|
+
readonly options: Span | null;
|
|
157
|
+
/** The closing fence's line, or `null` for a block that runs to the end of the note. */
|
|
158
|
+
readonly close: Span | null;
|
|
159
|
+
/** What it holds, from the line after its options to the line before its closing fence. */
|
|
160
|
+
readonly content: Span;
|
|
161
|
+
readonly style: NoteStyle;
|
|
162
|
+
readonly depth: number;
|
|
163
|
+
}
|
|
164
|
+
/** The style blocks `findStyleBlocks` found in a text, by offsets into it. */
|
|
165
|
+
export declare function styleSpans(text: string, found?: StyleBlocks): StyleBlockSpan[];
|
|
147
166
|
export type ConstructKind = Construct['kind'];
|
|
148
167
|
/** The constructs of a note, in the order of their `from`. */
|
|
149
168
|
export interface Analysis {
|
package/dist/edit/analyze.js
CHANGED
|
@@ -16,9 +16,32 @@ import remarkMath from 'remark-math';
|
|
|
16
16
|
import remarkParse from 'remark-parse';
|
|
17
17
|
import { unified } from 'unified';
|
|
18
18
|
import { codeRegions, commentRegions, insideCode } from '../source.js';
|
|
19
|
+
import { justifyColumns } from '../justified.js';
|
|
20
|
+
import { findStyleBlocks } from '../style-block.js';
|
|
19
21
|
import { CALLOUT_MARKER } from '../remark-callouts.js';
|
|
20
22
|
import { breaksDollarRule } from '../remark-ring.js';
|
|
21
23
|
import { hasParameterReference, parameterReferences } from '../parameters.js';
|
|
24
|
+
/** The style blocks `findStyleBlocks` found in a text, by offsets into it. */
|
|
25
|
+
export function styleSpans(text, found = findStyleBlocks(text)) {
|
|
26
|
+
const starts = [0];
|
|
27
|
+
for (let index = 0; index < text.length; index++)
|
|
28
|
+
if (text[index] === '\n')
|
|
29
|
+
starts.push(index + 1);
|
|
30
|
+
const lineSpan = (line) => {
|
|
31
|
+
const from = starts[line - 1] ?? text.length;
|
|
32
|
+
const next = starts[line];
|
|
33
|
+
return { from, to: next === undefined ? text.length : next - 1 };
|
|
34
|
+
};
|
|
35
|
+
return found.blocks.map((block) => {
|
|
36
|
+
const open = lineSpan(block.open);
|
|
37
|
+
const close = block.close === null ? null : lineSpan(block.close);
|
|
38
|
+
// With options, the content starts two lines past their first `---` at least.
|
|
39
|
+
const options = block.from > block.open + 1 ? { from: lineSpan(block.open + 1).from, to: lineSpan(block.from - 1).to } : null;
|
|
40
|
+
const contentFrom = Math.min((options ?? open).to + 1, text.length);
|
|
41
|
+
const contentTo = close ? Math.max(contentFrom, close.from - 1) : text.length;
|
|
42
|
+
return { from: open.from, to: close ? close.to : text.length, open, options, close, content: { from: contentFrom, to: contentTo }, style: block.style, depth: block.depth };
|
|
43
|
+
});
|
|
44
|
+
}
|
|
22
45
|
let parser = null;
|
|
23
46
|
function parse(text) {
|
|
24
47
|
// The micromark extensions of `readingPlugins`, at its options; the transforms that rewrite the
|
|
@@ -56,7 +79,13 @@ export function analyze(text) {
|
|
|
56
79
|
if (front)
|
|
57
80
|
found.push({ kind: 'frontmatter', ...front });
|
|
58
81
|
// The body is parsed alone, padded so that every offset is an offset of the note.
|
|
59
|
-
|
|
82
|
+
// A style block's fences and options leave the text, and a justified column is read as the reading
|
|
83
|
+
// surface reads it, every offset kept (§7.16, §4.1).
|
|
84
|
+
const padded = '\n'.repeat(bodyFrom) + text.slice(bodyFrom);
|
|
85
|
+
const styled = findStyleBlocks(padded);
|
|
86
|
+
for (const block of styleSpans(padded, styled))
|
|
87
|
+
found.push({ kind: 'styleBlock', ...block });
|
|
88
|
+
const tree = parse(justifyColumns(styled.text).text);
|
|
60
89
|
const comments = commentRegions(text).filter(([from]) => from >= bodyFrom);
|
|
61
90
|
for (const [from, to] of comments)
|
|
62
91
|
found.push({ kind: 'comment', from, to });
|
package/dist/edit/blocks.d.ts
CHANGED
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { EditorView } from '@codemirror/view';
|
|
11
11
|
import { type ReactNode } from 'react';
|
|
12
|
+
import { type NoteStyle } from '../note-style.js';
|
|
13
|
+
/**
|
|
14
|
+
* The opening of a style block while the author is elsewhere (`SPEC.md` §7.16, #166): what it sets,
|
|
15
|
+
* as the form says it, in place of its fence and options. A click shows them as written; the
|
|
16
|
+
* toolbar's Style changes them.
|
|
17
|
+
*/
|
|
18
|
+
export declare function StyleSummary({ style }: {
|
|
19
|
+
style: NoteStyle;
|
|
20
|
+
}): import("react").JSX.Element;
|
|
12
21
|
/** A run of notation drawn as the reading surface draws it: comments out, wikilinks resolved, embeds expanded. */
|
|
13
22
|
export declare function Snippet({ text }: {
|
|
14
23
|
text: string;
|
|
@@ -92,6 +101,7 @@ export declare const ICONS: {
|
|
|
92
101
|
readonly alignLeft: ReactNode;
|
|
93
102
|
readonly alignCenter: ReactNode;
|
|
94
103
|
readonly alignRight: ReactNode;
|
|
104
|
+
readonly alignJustify: ReactNode;
|
|
95
105
|
readonly undo: ReactNode;
|
|
96
106
|
readonly redo: ReactNode;
|
|
97
107
|
readonly bold: ReactNode;
|
|
@@ -111,6 +121,7 @@ export declare const ICONS: {
|
|
|
111
121
|
readonly math: ReactNode;
|
|
112
122
|
readonly diagram: ReactNode;
|
|
113
123
|
readonly rule: ReactNode;
|
|
124
|
+
readonly style: ReactNode;
|
|
114
125
|
readonly footnote: ReactNode;
|
|
115
126
|
readonly comment: ReactNode;
|
|
116
127
|
readonly caret: ReactNode;
|
package/dist/edit/blocks.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
/**
|
|
3
3
|
* The blocks the editor draws in place of their text while the author is not editing it, each
|
|
4
4
|
* drawn by the reading surface's own components so that edit mode looks like the page (ADR-006):
|
|
@@ -17,6 +17,7 @@ import { isTrue, readProperties } from '../properties.js';
|
|
|
17
17
|
import { Transclusion } from '../Transclusion.js';
|
|
18
18
|
import { splitEmbeds } from '../embeds.js';
|
|
19
19
|
import { embedsFile } from '../embed-target.js';
|
|
20
|
+
import { STYLE_KEYS } from '../note-style.js';
|
|
20
21
|
import { wikilinkAddress } from '../wikilink-address.js';
|
|
21
22
|
import { ICON_OWN_SIZE, ICON_RATIO_FREE, ICON_RATIO_LOCKED, ICON_RESIZE } from '../mode-icons.js';
|
|
22
23
|
import { describeImage, MAX_IMAGE_WIDTH, readImageSize, resizeImage } from './image-size.js';
|
|
@@ -48,6 +49,25 @@ function write(view, changes, event = 'input.form') {
|
|
|
48
49
|
writing = false;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* The opening of a style block while the author is elsewhere (`SPEC.md` §7.16, #166): what it sets,
|
|
54
|
+
* as the form says it, in place of its fence and options. A click shows them as written; the
|
|
55
|
+
* toolbar's Style changes them.
|
|
56
|
+
*/
|
|
57
|
+
export function StyleSummary({ style }) {
|
|
58
|
+
const words = useHost().strings.editor;
|
|
59
|
+
const label = { font: words.styleFont, headings: words.styleHeadings, text: words.styleText, tables: words.styleTables };
|
|
60
|
+
const value = {
|
|
61
|
+
sans: words.styleSans,
|
|
62
|
+
serif: words.styleSerif,
|
|
63
|
+
left: words.styleLeft,
|
|
64
|
+
center: words.styleCenter,
|
|
65
|
+
right: words.styleRight,
|
|
66
|
+
justify: words.styleJustify,
|
|
67
|
+
};
|
|
68
|
+
const set = STYLE_KEYS.filter((key) => style[key]);
|
|
69
|
+
return (_jsxs("div", { className: "edit-style-summary", children: [_jsxs("span", { className: "edit-style-summary-title", children: [ICONS.style, " ", words.style] }), set.length === 0 ? (_jsx("span", { className: "edit-style-summary-item", children: words.styleNotebook })) : (set.map((key) => (_jsxs("span", { className: "edit-style-summary-item", children: [label[key], ": ", value[style[key]]] }, key))))] }));
|
|
70
|
+
}
|
|
51
71
|
/** A run of notation drawn as the reading surface draws it: comments out, wikilinks resolved, embeds expanded. */
|
|
52
72
|
export function Snippet({ text }) {
|
|
53
73
|
const host = useHost();
|
|
@@ -745,7 +765,7 @@ export function TableBlock({ view, from, to }) {
|
|
|
745
765
|
};
|
|
746
766
|
const align = (alignment) => act((current) => alignColumn(current, focus.column, alignment));
|
|
747
767
|
const bodyRows = table.rows.slice(2);
|
|
748
|
-
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.source, onClick: () => {
|
|
768
|
+
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: () => {
|
|
749
769
|
act(() => []);
|
|
750
770
|
reveal(view, table.from);
|
|
751
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))) }))] }) })] }));
|
|
@@ -971,6 +991,7 @@ export const ICONS = {
|
|
|
971
991
|
alignLeft: icon('M21 6H3', 'M15 12H3', 'M17 18H3'),
|
|
972
992
|
alignCenter: icon('M21 6H3', 'M17 12H7', 'M19 18H5'),
|
|
973
993
|
alignRight: icon('M21 6H3', 'M21 12H9', 'M21 18H7'),
|
|
994
|
+
alignJustify: icon('M3 6h18', 'M3 12h18', 'M3 18h18'),
|
|
974
995
|
undo: icon('M9 14 4 9l5-5', 'M4 9h10.5a5.5 5.5 0 0 1 5.5 5.5 5.5 5.5 0 0 1-5.5 5.5H11'),
|
|
975
996
|
redo: icon('m15 14 5-5-5-5', 'M20 9H9.5A5.5 5.5 0 0 0 4 14.5 5.5 5.5 0 0 0 9.5 20H13'),
|
|
976
997
|
bold: icon('M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8'),
|
|
@@ -990,6 +1011,7 @@ export const ICONS = {
|
|
|
990
1011
|
math: icon('M18 7V5a1 1 0 0 0-1-1H6.5a.5.5 0 0 0-.4.8l4.5 6a2 2 0 0 1 0 2.4l-4.5 6a.5.5 0 0 0 .4.8H17a1 1 0 0 0 1-1v-2'),
|
|
991
1012
|
diagram: icon('M5 3h4v4H5z', 'M15 17h4v4h-4z', 'M7 7v4a2 2 0 0 0 2 2h6a2 2 0 0 1 2 2v2'),
|
|
992
1013
|
rule: icon('M5 12h14'),
|
|
1014
|
+
style: icon('M4 7V4h16v3', 'M9 20h6', 'M12 4v16'),
|
|
993
1015
|
footnote: icon('M4 7V4h16v3', 'M9 20h6', 'M12 4v16', 'M19 13v6', 'M17 15l2-2'),
|
|
994
1016
|
comment: icon('M7.9 20A9 9 0 1 0 4 16.1L2 22Z'),
|
|
995
1017
|
caret: icon('m6 9 6 6 6-6'),
|
package/dist/edit/heights.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export declare function lineHeightOf(kind: 'text' | 'code' | {
|
|
|
59
59
|
readonly heading: number;
|
|
60
60
|
}, length: number, metrics: BlockMetrics): number | null;
|
|
61
61
|
/** What a block is, for its estimate. */
|
|
62
|
-
export type BlockKind = 'frontmatter' | 'callout' | 'table' | 'embed' | 'diagram' | 'preview' | 'add-properties' | 'image';
|
|
62
|
+
export type BlockKind = 'frontmatter' | 'callout' | 'table' | 'embed' | 'diagram' | 'preview' | 'add-properties' | 'image' | 'style';
|
|
63
63
|
/** The height a block of this kind and these lines is drawn at, near enough to lay out the page. */
|
|
64
64
|
export declare function estimateHeight(kind: BlockKind, source: string, metrics: BlockMetrics): number;
|
|
65
65
|
/** The height a block of this content was drawn at at this width, if it was. */
|
package/dist/edit/heights.js
CHANGED
|
@@ -129,6 +129,9 @@ export function estimateHeight(kind, source, metrics) {
|
|
|
129
129
|
return Math.round(8 * lineHeight);
|
|
130
130
|
case 'diagram':
|
|
131
131
|
return Math.round(12 * lineHeight);
|
|
132
|
+
case 'style':
|
|
133
|
+
// The options of a style block, drawn as one line of what it sets (#166).
|
|
134
|
+
return Math.round(1.6 * lineHeight);
|
|
132
135
|
case 'preview':
|
|
133
136
|
return Math.round(lineHeight + wrapped(lines, metrics.width, metrics) * lineHeight);
|
|
134
137
|
case 'add-properties':
|
package/dist/edit/live.js
CHANGED
|
@@ -19,7 +19,8 @@ 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, TableBlock } from './blocks.js';
|
|
22
|
+
import { AddProperties, CalloutBlock, ImageBlock, PropertiesBlock, reveal, Snippet, StyleSummary, TableBlock } from './blocks.js';
|
|
23
|
+
import { justifies, styleProperties } from '../note-style.js';
|
|
23
24
|
import { caretAt, sourceOffsetOf } from './click-source.js';
|
|
24
25
|
import { LanguageControl } from './LanguageControl.js';
|
|
25
26
|
import { toggleTaskBox } from './commands.js';
|
|
@@ -249,6 +250,11 @@ class BlockWidget extends WidgetType {
|
|
|
249
250
|
toDOM(view) {
|
|
250
251
|
const element = document.createElement(this.inline ? 'span' : 'div');
|
|
251
252
|
element.className = this.inline ? 'cm-ms-inline-block' : 'cm-ms-block';
|
|
253
|
+
// A block a style block holds is drawn in its style, as its lines are (#166).
|
|
254
|
+
for (const [name, value] of Object.entries(this.options.style?.properties ?? {}))
|
|
255
|
+
element.style.setProperty(name, value);
|
|
256
|
+
if (this.options.style?.lang)
|
|
257
|
+
element.lang = this.options.style.lang;
|
|
252
258
|
drawing.set(element, this);
|
|
253
259
|
const metrics = view.state.field(metricsField);
|
|
254
260
|
const held = this.inline ? undefined : measuredHeight(this.identity, metrics);
|
|
@@ -397,6 +403,21 @@ class TaskWidget extends WidgetType {
|
|
|
397
403
|
const RULE_HEIGHT = 33;
|
|
398
404
|
const MATH_ROW = 32;
|
|
399
405
|
const MATH_MARGINS = 32;
|
|
406
|
+
/** The closing fence of a style block, drawn as the end of what it holds (#166). */
|
|
407
|
+
class StyleEndWidget extends WidgetType {
|
|
408
|
+
eq() {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
toDOM() {
|
|
412
|
+
const end = document.createElement('div');
|
|
413
|
+
end.className = 'cm-ms-style-end';
|
|
414
|
+
end.setAttribute('aria-hidden', 'true');
|
|
415
|
+
return end;
|
|
416
|
+
}
|
|
417
|
+
get estimatedHeight() {
|
|
418
|
+
return 8;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
400
421
|
class RuleWidget extends WidgetType {
|
|
401
422
|
eq() {
|
|
402
423
|
return true;
|
|
@@ -490,10 +511,30 @@ function build(state) {
|
|
|
490
511
|
for (let at = doc.lineAt(lines.from).number; at <= doc.lineAt(lines.to).number; at++)
|
|
491
512
|
out.push(line(className).range(doc.line(at).from));
|
|
492
513
|
};
|
|
514
|
+
// The style blocks of the note (§7.16, #166): what each line they hold is set as, the innermost
|
|
515
|
+
// block winning, written on the line, and on a block drawn in place of lines.
|
|
516
|
+
const styleBlocks = constructs.flatMap((construct) => (construct.kind === 'styleBlock' ? [construct] : []));
|
|
517
|
+
const styleOfLine = new Map();
|
|
518
|
+
if (styleBlocks.length > 0) {
|
|
519
|
+
const merged = new Map();
|
|
520
|
+
for (const styleBlock of [...styleBlocks].sort((a, b) => a.depth - b.depth)) {
|
|
521
|
+
if (styleBlock.content.from > styleBlock.content.to || styleBlock.content.from >= doc.length)
|
|
522
|
+
continue;
|
|
523
|
+
const first = doc.lineAt(styleBlock.content.from).number;
|
|
524
|
+
const last = doc.lineAt(Math.min(styleBlock.content.to, doc.length)).number;
|
|
525
|
+
for (let at = first; at <= last; at++)
|
|
526
|
+
merged.set(at, { ...(merged.get(at) ?? {}), ...styleBlock.style });
|
|
527
|
+
}
|
|
528
|
+
for (const [at, style] of merged) {
|
|
529
|
+
const properties = styleProperties(style);
|
|
530
|
+
styleOfLine.set(at, { properties, lang: justifies(style) ? host.strings.locale : null, css: Object.entries(properties).map(([name, value]) => `${name}:${value}`).join(';') });
|
|
531
|
+
}
|
|
532
|
+
}
|
|
493
533
|
const block = (from, to, kind, key, draw, revealsOnClick = true) => {
|
|
494
534
|
const lines = linesOf(from, to);
|
|
495
535
|
const source = text.slice(lines.from, lines.to);
|
|
496
|
-
|
|
536
|
+
const style = styleOfLine.get(doc.lineAt(lines.from).number);
|
|
537
|
+
out.push(Decoration.replace({ widget: new BlockWidget(style ? `${key}\u0000${style.css}` : key, host, draw, revealsOnClick, { source, kind, metrics, ...(style ? { style } : {}) }), block: true }).range(lines.from, lines.to));
|
|
497
538
|
covered.push(lines);
|
|
498
539
|
};
|
|
499
540
|
const after = (to, key, draw, source) => {
|
|
@@ -503,6 +544,31 @@ function build(state) {
|
|
|
503
544
|
if (bodyFrom === 0) {
|
|
504
545
|
out.push(Decoration.widget({ widget: new BlockWidget('add-properties', host, (view) => createElement(AddProperties, { view }), false, { kind: 'add-properties', metrics }), block: true, side: -1 }).range(0));
|
|
505
546
|
}
|
|
547
|
+
// A style block's fences and options are drawn as what it sets while the author is elsewhere, and
|
|
548
|
+
// as their lines when the author is on them (§7.16, #166).
|
|
549
|
+
for (const styleBlock of styleBlocks) {
|
|
550
|
+
const notation = { from: styleBlock.open.from, to: styleBlock.options?.to ?? styleBlock.open.to };
|
|
551
|
+
const open = touches(notation.from, notation.to) || (styleBlock.close !== null && touches(styleBlock.close.from, styleBlock.close.to));
|
|
552
|
+
if (open) {
|
|
553
|
+
eachLine(notation.from, notation.to, 'cm-ms-style-source');
|
|
554
|
+
if (styleBlock.close)
|
|
555
|
+
eachLine(styleBlock.close.from, styleBlock.close.to, 'cm-ms-style-source');
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const source = text.slice(notation.from, notation.to);
|
|
559
|
+
const style = styleBlock.style;
|
|
560
|
+
out.push(Decoration.replace({ widget: new BlockWidget(`style:${notation.from}:${source}`, host, () => createElement(StyleSummary, { style }), true, { source, kind: 'style', metrics }), block: true }).range(notation.from, notation.to));
|
|
561
|
+
covered.push(notation);
|
|
562
|
+
if (styleBlock.close) {
|
|
563
|
+
out.push(Decoration.replace({ widget: new StyleEndWidget(), block: true }).range(styleBlock.close.from, styleBlock.close.to));
|
|
564
|
+
covered.push({ from: styleBlock.close.from, to: styleBlock.close.to });
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
for (const [at, style] of styleOfLine) {
|
|
568
|
+
if (at > doc.lines)
|
|
569
|
+
continue;
|
|
570
|
+
out.push(Decoration.line({ attributes: { class: 'cm-ms-styled', style: style.css, ...(style.lang ? { lang: style.lang } : {}) } }).range(doc.line(at).from));
|
|
571
|
+
}
|
|
506
572
|
// Blocks first: what a drawn block covers is not decorated again.
|
|
507
573
|
for (const construct of constructs) {
|
|
508
574
|
if (!BLOCK_KINDS.has(construct.kind) || inCovered(construct.from, construct.to))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `:::{style}` block written from the editor's controls (`SPEC.md` §7.16, #166): wrapping a
|
|
3
|
+
* passage, and changing the options of the block the cursor is in, as changes to the note's bytes
|
|
4
|
+
* that touch nothing else. No React and no DOM.
|
|
5
|
+
*
|
|
6
|
+
* A block never opens above the frontmatter: a selection that takes it in, the whole note
|
|
7
|
+
* included, opens the block on the line after its closing `---` (§6.1), since a note whose first
|
|
8
|
+
* line is the fence has no frontmatter at all.
|
|
9
|
+
*/
|
|
10
|
+
import { type NoteStyle } from '../note-style.js';
|
|
11
|
+
import { type StyleBlockSpan } from './analyze.js';
|
|
12
|
+
import type { TextChange } from './table.js';
|
|
13
|
+
/** Every style block of the note, by offsets. */
|
|
14
|
+
export declare function styleBlocks(text: string): StyleBlockSpan[];
|
|
15
|
+
/** The innermost style block that holds an offset, its fences included, or `null`. */
|
|
16
|
+
export declare function styleBlockAt(text: string, at: number): StyleBlockSpan | null;
|
|
17
|
+
/** The options of a style, as its block writes them: only the keys it sets, in their order. */
|
|
18
|
+
export declare function optionsText(style: NoteStyle): string;
|
|
19
|
+
/** Changes a block's options to `style`: written, rewritten, or taken out when it sets nothing. */
|
|
20
|
+
export declare function setStyleOptions(block: StyleBlockSpan, style: NoteStyle): TextChange[];
|
|
21
|
+
/**
|
|
22
|
+
* Wraps a passage in a style block: the lines `from` to `to` take in, or with no selection the
|
|
23
|
+
* paragraph or block the cursor is in, up to the blank lines around it. Answers the changes and
|
|
24
|
+
* where the cursor goes, at the start of the content.
|
|
25
|
+
*/
|
|
26
|
+
export declare function wrapInStyle(text: string, from: number, to: number, style: NoteStyle): {
|
|
27
|
+
changes: TextChange[];
|
|
28
|
+
cursor: number;
|
|
29
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `:::{style}` block written from the editor's controls (`SPEC.md` §7.16, #166): wrapping a
|
|
3
|
+
* passage, and changing the options of the block the cursor is in, as changes to the note's bytes
|
|
4
|
+
* that touch nothing else. No React and no DOM.
|
|
5
|
+
*
|
|
6
|
+
* A block never opens above the frontmatter: a selection that takes it in, the whole note
|
|
7
|
+
* included, opens the block on the line after its closing `---` (§6.1), since a note whose first
|
|
8
|
+
* line is the fence has no frontmatter at all.
|
|
9
|
+
*/
|
|
10
|
+
import { STYLE_KEYS } from '../note-style.js';
|
|
11
|
+
import { frontmatterSpan, styleSpans } from './analyze.js';
|
|
12
|
+
/** Every style block of the note, by offsets. */
|
|
13
|
+
export function styleBlocks(text) {
|
|
14
|
+
return styleSpans(text);
|
|
15
|
+
}
|
|
16
|
+
/** The innermost style block that holds an offset, its fences included, or `null`. */
|
|
17
|
+
export function styleBlockAt(text, at) {
|
|
18
|
+
return (styleBlocks(text)
|
|
19
|
+
.filter((block) => block.from <= at && at <= block.to)
|
|
20
|
+
.sort((a, b) => b.depth - a.depth)[0] ?? null);
|
|
21
|
+
}
|
|
22
|
+
/** The options of a style, as its block writes them: only the keys it sets, in their order. */
|
|
23
|
+
export function optionsText(style) {
|
|
24
|
+
const lines = STYLE_KEYS.flatMap((key) => (style[key] ? [`${key}: ${style[key]}`] : []));
|
|
25
|
+
return lines.length === 0 ? '' : `---\n${lines.join('\n')}\n---`;
|
|
26
|
+
}
|
|
27
|
+
/** Changes a block's options to `style`: written, rewritten, or taken out when it sets nothing. */
|
|
28
|
+
export function setStyleOptions(block, style) {
|
|
29
|
+
const options = optionsText(style);
|
|
30
|
+
if (block.options) {
|
|
31
|
+
// Taken out with the line break before them, so the opening fence is followed by the content.
|
|
32
|
+
return options ? [{ from: block.options.from, to: block.options.to, insert: options }] : [{ from: block.open.to, to: block.options.to, insert: '' }];
|
|
33
|
+
}
|
|
34
|
+
return options ? [{ from: block.open.to, to: block.open.to, insert: `\n${options}` }] : [];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Wraps a passage in a style block: the lines `from` to `to` take in, or with no selection the
|
|
38
|
+
* paragraph or block the cursor is in, up to the blank lines around it. Answers the changes and
|
|
39
|
+
* where the cursor goes, at the start of the content.
|
|
40
|
+
*/
|
|
41
|
+
export function wrapInStyle(text, from, to, style) {
|
|
42
|
+
const body = frontmatterSpan(text)?.to ?? 0;
|
|
43
|
+
let start = Math.max(from, body);
|
|
44
|
+
let end = Math.max(to, start);
|
|
45
|
+
const lineStart = (at) => text.lastIndexOf('\n', at - 1) + 1;
|
|
46
|
+
const lineEnd = (at) => (text.indexOf('\n', at) === -1 ? text.length : text.indexOf('\n', at));
|
|
47
|
+
const blank = (at) => text.slice(lineStart(at), lineEnd(at)).trim() === '';
|
|
48
|
+
if (start === end && !blank(start)) {
|
|
49
|
+
// The paragraph or block the cursor is in: back and forth to the blank lines around it.
|
|
50
|
+
start = lineStart(start);
|
|
51
|
+
while (start > body && !blank(start - 1))
|
|
52
|
+
start = lineStart(start - 1);
|
|
53
|
+
end = lineEnd(end);
|
|
54
|
+
while (end < text.length && !blank(end + 1))
|
|
55
|
+
end = lineEnd(end + 1);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
start = lineStart(start);
|
|
59
|
+
// A selection that ends at the start of a line ends on the line before it.
|
|
60
|
+
end = end > start && text[end - 1] === '\n' ? end - 1 : lineEnd(end);
|
|
61
|
+
}
|
|
62
|
+
const options = optionsText(style);
|
|
63
|
+
const before = start > 0 && text[start - 1] !== '\n' ? '\n' : '';
|
|
64
|
+
const opening = `${before}:::{style}\n${options ? `${options}\n` : ''}`;
|
|
65
|
+
const empty = start === end;
|
|
66
|
+
const changes = [
|
|
67
|
+
{ from: start, to: start, insert: empty ? `${opening}\n:::` : opening },
|
|
68
|
+
...(empty ? [] : [{ from: end, to: end, insert: '\n:::' }]),
|
|
69
|
+
];
|
|
70
|
+
return { changes, cursor: start + opening.length };
|
|
71
|
+
}
|
package/dist/edit/table.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* A row an edit does not reach keeps its spacing and its pipes as written.
|
|
7
7
|
*/
|
|
8
8
|
import type { Span } from './analyze.js';
|
|
9
|
-
export type Alignment = 'left' | 'center' | 'right' | null;
|
|
9
|
+
export type Alignment = 'left' | 'center' | 'right' | 'justify' | null;
|
|
10
10
|
export interface TableCell extends Span {
|
|
11
11
|
/** The content as written, trimmed: the range is the content's, without the padding. */
|
|
12
12
|
readonly text: string;
|