Added automatic quote insertion (#897)
parent
e535ef14ff
commit
2a2cdef0f4
|
@ -1,5 +1,6 @@
|
||||||
import { KeyBinding } from "@codemirror/view";
|
import { KeyBinding } from "@codemirror/view";
|
||||||
import { syntaxTree } from "@codemirror/language";
|
import { syntaxTree } from "@codemirror/language";
|
||||||
|
import { EditorSelection } from "@codemirror/state";
|
||||||
|
|
||||||
const straightQuoteContexts = [
|
const straightQuoteContexts = [
|
||||||
"CommentBlock",
|
"CommentBlock",
|
||||||
|
@ -14,7 +15,7 @@ const straightQuoteContexts = [
|
||||||
|
|
||||||
// TODO: Add support for selection (put quotes around or create blockquote block?)
|
// TODO: Add support for selection (put quotes around or create blockquote block?)
|
||||||
function keyBindingForQuote(
|
function keyBindingForQuote(
|
||||||
quote: string,
|
originalQuote: string,
|
||||||
left: string,
|
left: string,
|
||||||
right: string,
|
right: string,
|
||||||
): KeyBinding {
|
): KeyBinding {
|
||||||
|
@ -22,7 +23,7 @@ function keyBindingForQuote(
|
||||||
any: (target, event): boolean => {
|
any: (target, event): boolean => {
|
||||||
// Moving this check here rather than using the regular "key" property because
|
// Moving this check here rather than using the regular "key" property because
|
||||||
// for some reason the "ä" key is not recognized as a quote key by CodeMirror.
|
// for some reason the "ä" key is not recognized as a quote key by CodeMirror.
|
||||||
if (event.key !== quote) {
|
if (event.key !== originalQuote) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const cursorPos = target.state.selection.main.from;
|
const cursorPos = target.state.selection.main.from;
|
||||||
|
@ -42,19 +43,36 @@ function keyBindingForQuote(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ok, still here, let's use a smart quote
|
// Ok, still here, let's use a smart quote
|
||||||
let q = right;
|
const changes = target.state.changeByRange((range) => {
|
||||||
if (/\W/.exec(chBefore) && !/[!\?,\.\-=“]/.exec(chBefore)) {
|
if (!range.empty) {
|
||||||
q = left;
|
return {
|
||||||
}
|
changes: [
|
||||||
target.dispatch({
|
{ insert: left, from: range.from },
|
||||||
|
{ insert: right, from: range.to },
|
||||||
|
],
|
||||||
|
range: EditorSelection.range(
|
||||||
|
range.anchor + left.length,
|
||||||
|
range.head + left.length,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const quote = (/\W/.exec(chBefore) && !/[!\?,\.\-=“]/.exec(chBefore))
|
||||||
|
? left
|
||||||
|
: right;
|
||||||
|
|
||||||
|
return {
|
||||||
changes: {
|
changes: {
|
||||||
insert: q,
|
insert: quote,
|
||||||
from: cursorPos,
|
from: cursorPos,
|
||||||
},
|
},
|
||||||
selection: {
|
range: EditorSelection.cursor(
|
||||||
anchor: cursorPos + 1,
|
range.anchor + quote.length,
|
||||||
},
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
target.dispatch(changes);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue