Improve editor, plugin updates, and window transfers for 0.1.41
This commit is contained in:
@@ -145,6 +145,24 @@ class Preview extends WidgetType {
|
||||
}
|
||||
|
||||
export function liveMarkdown(urls: Map<string, string>, actions: TableContextActions = {}, images: ImageContext = {}) {
|
||||
const focusBody = StateEffect.define<boolean>();
|
||||
const bodyFocused = StateField.define({
|
||||
create: () => false,
|
||||
update(value, transaction) {
|
||||
for (const effect of transaction.effects) if (effect.is(focusBody)) value = effect.value;
|
||||
return value;
|
||||
},
|
||||
});
|
||||
const syncBodyFocus = ViewPlugin.define(view => {
|
||||
// A restored editor can already own focus before its focus observer starts.
|
||||
const sync = () => queueMicrotask(() => {
|
||||
if (view.dom.isConnected && view.state.field(bodyFocused, false) !== undefined && view.state.field(bodyFocused) !== view.hasFocus) {
|
||||
view.dispatch({ effects: focusBody.of(view.hasFocus) });
|
||||
}
|
||||
});
|
||||
sync();
|
||||
return { update: sync };
|
||||
});
|
||||
function build(state: EditorState, previous?: DecorationSet): DecorationSet {
|
||||
const ranges: Range<Decoration>[] = [];
|
||||
const active = state.selection.ranges.map(range => ({ from: state.doc.lineAt(range.from).from, to: state.doc.lineAt(range.to).to }));
|
||||
@@ -221,17 +239,32 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
return false;
|
||||
}
|
||||
const line = state.doc.lineAt(node.from);
|
||||
if (/^(?:ATX|Setext)Heading[1-6]$/.test(node.name)) {
|
||||
// A list starts with one hyphen. Do not enlarge the preceding paragraph
|
||||
// while the user enters that marker. Keep the Markdown source unchanged.
|
||||
const pendingList = node.name === "SetextHeading2" && state.doc.lineAt(node.to).text.trim() === "-";
|
||||
if (/^(?:ATX|Setext)Heading[1-6]$/.test(node.name) && !pendingList) {
|
||||
ranges.push(Decoration.line({ class: `writer-heading writer-h${node.name.at(-1)}` }).range(line.from));
|
||||
}
|
||||
if (node.name === "InlineCode") codeRanges.push({ from: node.from, to: node.to });
|
||||
if (node.name === "TaskMarker") {
|
||||
const match = /^(\s*)([-+*]\s+)\[([ xX])\]\s?/.exec(line.text);
|
||||
if (!match) return;
|
||||
const markerFrom = line.from + match[1].length;
|
||||
// ArrowLeft and Home reach the replacement's start, before the list bullet.
|
||||
// Keep the trailing space outside the edit range so task text stays rendered.
|
||||
const editingMarker = state.field(bodyFocused) && state.selection.ranges.some(range => range.from <= node.to && range.to >= markerFrom);
|
||||
if (editingMarker) return;
|
||||
ranges.push(Decoration.replace({ widget: new Checkbox(match[3].toLowerCase() === "x"), atomic: true }).range(markerFrom, line.from + match[0].length));
|
||||
if (match[3].toLowerCase() === "x") mark(line.from + match[0].length, line.to, "writer-completed");
|
||||
return;
|
||||
}
|
||||
if (selected(node.from, node.to)) return;
|
||||
if (node.name === "HorizontalRule") {
|
||||
ranges.push(Decoration.line({ class: "writer-divider-line" }).range(line.from));
|
||||
ranges.push(Decoration.replace({ widget: new Divider(), atomic: true }).range(node.from, node.to));
|
||||
return false;
|
||||
}
|
||||
if (node.name === "HeaderMark") replace(node.from, Math.min(line.to, node.to + 1));
|
||||
if (node.name === "HeaderMark" && !(source.slice(node.from, node.to) === "-" && node.node.parent?.name === "SetextHeading2")) replace(node.from, Math.min(line.to, node.to + 1));
|
||||
if (node.name === "StrongEmphasis") mark(node.from, node.to, "writer-strong");
|
||||
if (node.name === "Emphasis") mark(node.from, node.to, "writer-emphasis");
|
||||
if (node.name === "Strikethrough") mark(node.from, node.to, "writer-strike");
|
||||
@@ -252,12 +285,6 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
ranges.push(Decoration.mark({ class: "writer-link", attributes: { "data-href": match[2], "data-kind": "md", "data-editor-tooltip": "Ctrl+click to open link" } }).range(node.from + 1, node.from + 1 + match[1].length));
|
||||
return false;
|
||||
}
|
||||
if (node.name === "TaskMarker") {
|
||||
const match = /^(\s*)([-+*]\s+)\[([ xX])\]\s?/.exec(line.text);
|
||||
if (!match) return;
|
||||
ranges.push(Decoration.replace({ widget: new Checkbox(match[3].toLowerCase() === "x"), atomic: true }).range(line.from + match[1].length, line.from + match[0].length));
|
||||
if (match[3].toLowerCase() === "x") mark(line.from + match[0].length, line.to, "writer-completed");
|
||||
}
|
||||
} });
|
||||
for (const match of source.matchAll(/\[\^([^\]\s]+)\](:)?/g)) {
|
||||
const from = match.index, to = from + match[0].length;
|
||||
@@ -281,12 +308,12 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
}
|
||||
return Decoration.set(ranges, true);
|
||||
}
|
||||
return [imageSource, linkTooltips, StateField.define<DecorationSet>({
|
||||
return [imageSource, linkTooltips, bodyFocused, syncBodyFocus, EditorView.focusChangeEffect.of((_state, focusing) => focusBody.of(focusing)), StateField.define<DecorationSet>({
|
||||
create: build,
|
||||
update(value, transaction) {
|
||||
return transaction.docChanged || transaction.selection
|
||||
|| syntaxTree(transaction.startState) !== syntaxTree(transaction.state)
|
||||
|| transaction.effects.some(effect => effect.is(refreshImages) || effect.is(editImageSource) || effect.is(foldEffect) || effect.is(unfoldEffect))
|
||||
|| transaction.effects.some(effect => effect.is(focusBody) || effect.is(refreshImages) || effect.is(editImageSource) || effect.is(foldEffect) || effect.is(unfoldEffect))
|
||||
? build(transaction.state, value.map(transaction.changes)) : value;
|
||||
},
|
||||
provide: field => [EditorView.decorations.from(field), EditorView.atomicRanges.of(view => view.state.field(field).update({
|
||||
|
||||
Reference in New Issue
Block a user