Fix spell checking for code blocks and inline code (#1244)
parent
31792a3a43
commit
f553c62950
|
@ -7,7 +7,6 @@ interface WrapElement {
|
||||||
selector: string;
|
selector: string;
|
||||||
class: string;
|
class: string;
|
||||||
nesting?: boolean;
|
nesting?: boolean;
|
||||||
disableSpellCheck?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function lineWrapper(wrapElements: WrapElement[]) {
|
export function lineWrapper(wrapElements: WrapElement[]) {
|
||||||
|
@ -18,10 +17,6 @@ export function lineWrapper(wrapElements: WrapElement[]) {
|
||||||
syntaxTree(state).iterate({
|
syntaxTree(state).iterate({
|
||||||
enter: ({ type, from, to }) => {
|
enter: ({ type, from, to }) => {
|
||||||
for (const wrapElement of wrapElements) {
|
for (const wrapElement of wrapElements) {
|
||||||
const spellCheckAttributes = wrapElement.disableSpellCheck
|
|
||||||
? { attributes: { spellcheck: "false" } }
|
|
||||||
: {};
|
|
||||||
|
|
||||||
if (type.name == wrapElement.selector) {
|
if (type.name == wrapElement.selector) {
|
||||||
if (wrapElement.nesting) {
|
if (wrapElement.nesting) {
|
||||||
elementStack.push(type.name);
|
elementStack.push(type.name);
|
||||||
|
@ -36,7 +31,6 @@ export function lineWrapper(wrapElements: WrapElement[]) {
|
||||||
widgets.push(
|
widgets.push(
|
||||||
Decoration.line({
|
Decoration.line({
|
||||||
class: cls,
|
class: cls,
|
||||||
...spellCheckAttributes,
|
|
||||||
}).range(doc.lineAt(idx).from),
|
}).range(doc.lineAt(idx).from),
|
||||||
);
|
);
|
||||||
idx += line.length + 1;
|
idx += line.length + 1;
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
import type { EditorState, Range } from "@codemirror/state";
|
||||||
|
import { Decoration } from "@codemirror/view";
|
||||||
|
import { syntaxTree } from "@codemirror/language";
|
||||||
|
import { decoratorStateField } from "./util.ts";
|
||||||
|
|
||||||
|
export function disableSpellcheck(selectors: string[]) {
|
||||||
|
return decoratorStateField((state: EditorState) => {
|
||||||
|
const widgets: Range<Decoration>[] = [];
|
||||||
|
syntaxTree(state).iterate({
|
||||||
|
enter: ({ type, from, to }) => {
|
||||||
|
for (const selector of selectors) {
|
||||||
|
if (type.name === selector) {
|
||||||
|
widgets.push(
|
||||||
|
Decoration.mark({
|
||||||
|
attributes: { spellcheck: "false" },
|
||||||
|
}).range(from, to),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return Decoration.set(widgets, true);
|
||||||
|
});
|
||||||
|
}
|
|
@ -53,6 +53,7 @@ import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts";
|
||||||
import { parseCommand } from "$common/command.ts";
|
import { parseCommand } from "$common/command.ts";
|
||||||
import { safeRun } from "$lib/async.ts";
|
import { safeRun } from "$lib/async.ts";
|
||||||
import { codeCopyPlugin } from "./cm_plugins/code_copy.ts";
|
import { codeCopyPlugin } from "./cm_plugins/code_copy.ts";
|
||||||
|
import { disableSpellcheck } from "./cm_plugins/spell_checking.ts";
|
||||||
|
|
||||||
export function createEditorState(
|
export function createEditorState(
|
||||||
client: Client,
|
client: Client,
|
||||||
|
@ -159,11 +160,7 @@ export function createEditorState(
|
||||||
{ selector: "Blockquote", class: "sb-line-blockquote" },
|
{ selector: "Blockquote", class: "sb-line-blockquote" },
|
||||||
{ selector: "Task", class: "sb-line-task" },
|
{ selector: "Task", class: "sb-line-task" },
|
||||||
{ selector: "CodeBlock", class: "sb-line-code" },
|
{ selector: "CodeBlock", class: "sb-line-code" },
|
||||||
{
|
{ selector: "FencedCode", class: "sb-line-fenced-code" },
|
||||||
selector: "FencedCode",
|
|
||||||
class: "sb-line-fenced-code",
|
|
||||||
disableSpellCheck: true,
|
|
||||||
},
|
|
||||||
{ selector: "Comment", class: "sb-line-comment" },
|
{ selector: "Comment", class: "sb-line-comment" },
|
||||||
{ selector: "BulletList", class: "sb-line-ul" },
|
{ selector: "BulletList", class: "sb-line-ul" },
|
||||||
{ selector: "OrderedList", class: "sb-line-ol" },
|
{ selector: "OrderedList", class: "sb-line-ol" },
|
||||||
|
@ -171,9 +168,9 @@ export function createEditorState(
|
||||||
{
|
{
|
||||||
selector: "FrontMatter",
|
selector: "FrontMatter",
|
||||||
class: "sb-frontmatter",
|
class: "sb-frontmatter",
|
||||||
disableSpellCheck: true,
|
|
||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
|
disableSpellcheck(["InlineCode", "CodeText", "FrontMatter"]),
|
||||||
keyBindings,
|
keyBindings,
|
||||||
EditorView.domEventHandlers({
|
EditorView.domEventHandlers({
|
||||||
// This may result in duplicated touch events on mobile devices
|
// This may result in duplicated touch events on mobile devices
|
||||||
|
|
Loading…
Reference in New Issue