-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (70 loc) · 2.54 KB
/
index.ts
File metadata and controls
81 lines (70 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import codemark from 'prosemirror-codemark';
import {Plugin} from 'prosemirror-state';
import type {Action, ExtensionAuto} from '../../../core';
import {
createMarkdownInlineMarkAction,
createMarkdownInlineMarkCommand,
} from '../../../utils/actions';
import {withLogAction} from '../../../utils/keymap';
import {CodeSpecs, codeType} from './CodeSpecs';
import './code.scss';
export {codeMarkName, codeType} from './CodeSpecs';
const codeAction = 'code';
export type CodeOptions = {
codeKey?: string | null;
};
export const Code: ExtensionAuto<CodeOptions> = (builder, opts) => {
builder.use(CodeSpecs);
builder.addAction(codeAction, ({schema}) => createMarkdownInlineMarkAction(codeType(schema)));
if (opts?.codeKey) {
const {codeKey} = opts;
builder.addKeymap(({schema}) => ({
[codeKey]: withLogAction(
'code_inline',
createMarkdownInlineMarkCommand(codeType(schema)),
),
}));
}
builder
// codemark adds inputRule for `code`,
// adds fake cursor behaviour when crossing inline_code boundaries,
// wrap current selected text to inline_code when '`' was pressed.
// See demo: https://curvenote.github.io/prosemirror-codemark/
.addPlugin(({schema}) => codemark({markType: codeType(schema)}));
builder.addPlugin(
() =>
// apply codemark when typing text between ``
new Plugin({
props: {
handleTextInput: (view, from, to, text) => {
const {
selection: {$anchor},
tr,
schema,
} = view.state;
if (
$anchor.nodeBefore?.text?.endsWith('`') &&
$anchor.nodeAfter?.text?.startsWith('`')
) {
view.dispatch(
tr.replaceRangeWith(
from - 1,
to + 1,
view.state.schema.text(text, [codeType(schema).create()]),
),
);
return true;
}
return false;
},
},
}),
);
};
declare global {
namespace WysiwygEditor {
interface Actions {
[codeAction]: Action;
}
}
}