fix: Backtick shortcut not applied correctly with composition (#6659)

* fix: Backtick shortcut not applied correctly with composition

* docs
This commit is contained in:
Tom Moor
2024-03-10 08:59:15 -06:00
committed by GitHub
parent fe4c2fb7d6
commit 9f55645655
2 changed files with 28 additions and 17 deletions

View File

@@ -19,7 +19,16 @@ function getMarksBetween(start: number, end: number, state: EditorState) {
return marks; return marks;
} }
export default function ( /**
* A factory function for creating Prosemirror plugins that automatically apply a mark to text
* that matches a given regular expression.
*
* @param regexp The regular expression to match
* @param markType The mark type to apply
* @param getAttrs A function that returns the attributes to apply to the mark
* @returns The input rule
*/
export default function markInputRule(
regexp: RegExp, regexp: RegExp,
markType: MarkType, markType: MarkType,
getAttrs?: (match: string[]) => Record<string, unknown> getAttrs?: (match: string[]) => Record<string, unknown>
@@ -29,15 +38,14 @@ export default function (
(state: EditorState, match: string[], start: number, end: number) => { (state: EditorState, match: string[], start: number, end: number) => {
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
const { tr } = state; const { tr } = state;
const m = match.length - 1; const captureGroup = match[match.length - 1];
let markEnd = end; const fullMatch = match[0];
let markStart = start; const startSpaces = fullMatch.search(/\S/);
if (match[m]) { if (captureGroup) {
const matchStart = start + match[0].indexOf(match[m - 1]); const matchStart = start + fullMatch.indexOf(captureGroup);
const matchEnd = matchStart + match[m - 1].length - 1; const textStart = start + fullMatch.lastIndexOf(captureGroup);
const textStart = matchStart + match[m - 1].lastIndexOf(match[m]); const textEnd = textStart + captureGroup.length;
const textEnd = textStart + match[m].length;
const excludedMarks = getMarksBetween(start, end, state) const excludedMarks = getMarksBetween(start, end, state)
.filter((item) => item.mark.type.excludes(markType)) .filter((item) => item.mark.type.excludes(markType))
@@ -47,17 +55,16 @@ export default function (
return null; return null;
} }
if (textEnd < matchEnd) { if (textEnd < end) {
tr.delete(textEnd, matchEnd); tr.delete(textEnd, end);
} }
if (textStart > matchStart) { if (textStart > start) {
tr.delete(matchStart, textStart); tr.delete(start + startSpaces, textStart);
} }
markStart = matchStart; end = start + startSpaces + captureGroup.length;
markEnd = markStart + match[m].length;
} }
tr.addMark(markStart, markEnd, markType.create(attrs)); tr.addMark(start + startSpaces, end, markType.create(attrs));
tr.removeStoredMark(markType); tr.removeStoredMark(markType);
return tr; return tr;
} }

View File

@@ -61,8 +61,12 @@ export default class Code extends Mark {
} }
get plugins() { get plugins() {
const codeCursorPlugin = codemark({
markType: this.editor.schema.marks.code_inline,
})[0];
return [ return [
...codemark({ markType: this.editor.schema.marks.code_inline }), codeCursorPlugin,
new Plugin({ new Plugin({
props: { props: {
// Typing a character inside of two backticks will wrap the character // Typing a character inside of two backticks will wrap the character