Update editor, workspace, Git services, appearance, and project guides
This commit is contained in:
@@ -9,6 +9,7 @@ import type { TableContextActions } from "./tableCellActions";
|
||||
import { imageControls } from "./imageControls";
|
||||
import { ImagePreview, type ImageContext } from "./imagePreview";
|
||||
import { imageSource, editImageSource, imageSourceRanges } from "./imageSource";
|
||||
import { actionIcon } from "../ui/actionIcons";
|
||||
|
||||
import { pluginHost } from "../plugins/registry.ts";
|
||||
|
||||
@@ -16,6 +17,40 @@ const previewFormulas = new WeakMap<HTMLElement, unknown>();
|
||||
export const refreshImages = StateEffect.define<void>();
|
||||
const imageCleanups = new WeakMap<HTMLElement, () => void>();
|
||||
|
||||
class CodeCopy extends WidgetType {
|
||||
constructor(readonly code: string) { super(); }
|
||||
eq(other: CodeCopy) { return this.code === other.code; }
|
||||
toDOM() {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "rs-fence-copy writer-code-copy";
|
||||
button.setAttribute("aria-label", "Copy code");
|
||||
button.title = "Copy code";
|
||||
const help = document.createElement("span");
|
||||
help.className = "writer-code-tooltip";
|
||||
help.setAttribute("role", "tooltip");
|
||||
help.textContent = "Copy code";
|
||||
button.append(actionIcon("copy"), help);
|
||||
button.onmousedown = event => event.preventDefault();
|
||||
button.onclick = async event => {
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
try {
|
||||
await navigator.clipboard.writeText(this.code);
|
||||
button.dataset.copied = "true";
|
||||
button.title = "Copied";
|
||||
help.textContent = "Copied";
|
||||
button.setAttribute("aria-label", "Code copied");
|
||||
} catch {
|
||||
button.title = "Could not copy code";
|
||||
help.textContent = "Could not copy code";
|
||||
button.setAttribute("aria-label", "Could not copy code");
|
||||
}
|
||||
};
|
||||
return button;
|
||||
}
|
||||
ignoreEvent() { return true; }
|
||||
}
|
||||
|
||||
class Divider extends WidgetType {
|
||||
eq(other: Divider) { return other instanceof Divider; }
|
||||
toDOM(view: EditorView) {
|
||||
@@ -109,6 +144,9 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
const imageNodes = imageSourceRanges(source);
|
||||
for (const section of renderMarkdownSections(source, urls)) {
|
||||
if (!/(?:<pre|<table|<img|md-properties|md-callout|missing-image)/.test(section.html)) continue;
|
||||
// Code remains a real editor surface. A preview swap loses the first drag
|
||||
// and changes block height when focus or the selection moves elsewhere.
|
||||
if (/^<pre\b/.test(section.html)) continue;
|
||||
// The first rendered section includes leading blank lines. Keep those
|
||||
// editable when the user exits above a table at the start of a note.
|
||||
if (/<table>/.test(section.html)) section.start += /^(?:[ \t]*\r?\n)+/.exec(source.slice(section.start, section.end))?.[0].length ?? 0;
|
||||
@@ -146,6 +184,10 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
ranges.push(Decoration.replace({ widget: new Preview(section.html, source.slice(section.start, end), actions, false, height), block: true, atomic: true }).range(section.start, end));
|
||||
}
|
||||
const hidden = (from: number, to: number) => hiddenBlocks.some(block => block.from <= from && block.to >= to);
|
||||
for (let number = 1; number <= state.doc.lines; number++) {
|
||||
const line = state.doc.line(number);
|
||||
if (!line.text.trim() && !hidden(line.from, line.to)) ranges.push(Decoration.line({ class: "writer-blank" }).range(line.from));
|
||||
}
|
||||
const replace = (from: number, to: number) => { if (to > from) ranges.push(Decoration.replace({ atomic: true }).range(from, to)); };
|
||||
const mark = (from: number, to: number, className: string) => { if (to > from) ranges.push(Decoration.mark({ class: className }).range(from, to)); };
|
||||
const codeRanges: { from: number; to: number }[] = [];
|
||||
@@ -153,7 +195,15 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
|
||||
if (hidden(node.from, node.to)) return false;
|
||||
if (node.name === "FencedCode" || node.name === "CodeBlock") {
|
||||
codeRanges.push({ from: node.from, to: node.to });
|
||||
mark(node.from, node.to, "writer-code-source");
|
||||
const first = state.doc.lineAt(node.from), last = state.doc.lineAt(node.to);
|
||||
const fenced = node.name === "FencedCode";
|
||||
const closed = fenced && /^\s*(`{3,}|~{3,})\s*$/.test(last.text) && last.number > first.number;
|
||||
const code = fenced ? source.slice(Math.min(first.to + 1, node.to), closed ? last.from : node.to) : source.slice(node.from, node.to);
|
||||
ranges.push(Decoration.widget({ widget: new CodeCopy(code), side: 1 }).range(first.to));
|
||||
for (let number = first.number; number <= last.number; number++) {
|
||||
const line = state.doc.line(number);
|
||||
ranges.push(Decoration.line({ class: `writer-code-line${number === first.number ? " writer-code-first" : ""}${number === last.number ? " writer-code-last" : ""}${fenced && (number === first.number || (closed && number === last.number)) ? " writer-code-fence" : ""}`, attributes: { spellcheck: "false" } }).range(line.from));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const line = state.doc.lineAt(node.from);
|
||||
|
||||
Reference in New Issue
Block a user