1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-31 14:28:06 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
c9d33c28d6 create indent-plugin 2022-09-19 16:28:45 +09:00
ba0206a4d2 disabled Ctrl-v scroll 2022-09-19 14:31:35 +09:00
c4568aebd7 fixed selection background color 2022-09-19 14:16:41 +09:00
4 changed files with 90 additions and 19 deletions

View file

@ -0,0 +1,79 @@
import { indentWithTab } from "@codemirror/commands";
import { indentService, indentUnit, syntaxTree } from "@codemirror/language";
import {
type DecorationSet,
type EditorView,
type ViewUpdate,
Decoration,
ViewPlugin,
WidgetType,
keymap,
} from "@codemirror/view";
class IndentWidget extends WidgetType {
constructor(readonly bullet: boolean) {
super();
}
toDOM() {
const indent = document.createElement("span");
indent.textContent = this.bullet ? "•" : " ";
indent.style.paddingInline = "0.5em";
return indent;
}
}
function indentDecorations(view: EditorView) {
const decorations: Array<{
from: number;
to: number;
value: Decoration;
}> = [];
for (const { from, to } of view.visibleRanges) {
syntaxTree(view.state).iterate({
from,
to,
enter(node) {
if (node.name === "Indent") {
const next = view.state.doc.sliceString(node.to, node.to + 1);
const decoration = Decoration.widget({
widget: new IndentWidget(/^[^ \t]?$/.test(next)),
});
decorations.push(decoration.range(node.from));
}
},
});
}
return Decoration.set(decorations);
}
const indentDecorationsPlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = indentDecorations(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.decorations = indentDecorations(update.view);
}
}
},
{
decorations(v) {
return v.decorations;
},
}
);
const indentPlugins = [
keymap.of([indentWithTab]),
indentUnit.of(" "),
indentService.of((context, pos) => {
const previousLine = context.lineAt(pos, -1).text;
if (previousLine.trim() === "") return null;
return /^[ \t]*/.exec(previousLine)?.[0]?.length ?? null;
}),
indentDecorationsPlugin,
];
export default indentPlugins;

View file

@ -11,7 +11,6 @@ export const quotLanguage = LRLanguage.define({
props: [ props: [
styleTags({ styleTags({
Heading: t.heading, Heading: t.heading,
Indent: t.separator,
AutoLink: t.link, AutoLink: t.link,
Code: t.monospace, Code: t.monospace,
}), }),
@ -27,21 +26,15 @@ export const quotHighlighting = syntaxHighlighting(
fontSize: "1.25em", fontSize: "1.25em",
marginBlockEnd: "0.5em", marginBlockEnd: "0.5em",
}, },
{
tag: t.separator,
letterSpacing: "1.5em",
"&:last-child:after": {
content: `"•"`,
marginInline: "-0.9em",
},
},
{ {
tag: t.link, tag: t.link,
class: "auto-link", class: "auto-link",
}, },
{ {
tag: t.monospace, tag: t.monospace,
background: "var(--nc-bg-3)", borderStyle: "solid",
borderColor: "var(--nc-bg-3)",
borderWidth: "1px",
borderRadius: "0.25em", borderRadius: "0.25em",
padding: "0.2em 0.4em", padding: "0.2em 0.4em",
fontSize: "0.9em", fontSize: "0.9em",

View file

@ -13,6 +13,10 @@
outline: unset; outline: unset;
} }
.editor .cm-editor.cm-focused .cm-selectionBackground {
background-color: var(--nc-ac-1);
}
.editor .cm-content { .editor .cm-content {
font-family: "sans-serif"; font-family: "sans-serif";
font-size: 1rem; font-size: 1rem;

View file

@ -1,11 +1,11 @@
import { onCleanup, onMount } from "solid-js"; import { onCleanup, onMount } from "solid-js";
import { minimalSetup } from "codemirror"; import { minimalSetup } from "codemirror";
import { emacsStyleKeymap, indentWithTab } from "@codemirror/commands"; import { emacsStyleKeymap } from "@codemirror/commands";
import { indentService, indentUnit } from "@codemirror/language";
import { EditorView, keymap } from "@codemirror/view"; import { EditorView, keymap } from "@codemirror/view";
import type Pages from "../../protocol/pages"; import type Pages from "../../protocol/pages";
import "./editor.css"; import "./editor.css";
import { quotHighlighting, quotLanguage } from "../../syntax/quot"; import { quotHighlighting, quotLanguage } from "../../syntax/quot";
import indentPlugins from "../../syntax/indent-plugins";
export default (props: { export default (props: {
id: number; id: number;
@ -36,16 +36,11 @@ export default (props: {
props.onUpdatePage({ id: props.id, title, text }); props.onUpdatePage({ id: props.id, title, text });
}), }),
EditorView.lineWrapping, EditorView.lineWrapping,
indentUnit.of(" "), keymap.of(emacsStyleKeymap.filter(({ key }) => key !== "Ctrl-v")),
indentService.of((context, pos) => {
const previousLine = context.lineAt(pos, -1).text;
if (previousLine.trim() === "") return null;
return /^[ \t]*/.exec(previousLine)?.[0]?.length ?? null;
}),
keymap.of([indentWithTab, ...emacsStyleKeymap]),
minimalSetup, minimalSetup,
quotLanguage, quotLanguage,
quotHighlighting, quotHighlighting,
...indentPlugins,
], ],
}); });
ref.addEventListener("click", (e) => { ref.addEventListener("click", (e) => {