@atlaskit/editor-plugin-code-block-advanced 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE.md +11 -0
- package/README.md +1 -0
- package/afm-cc/tsconfig.json +36 -0
- package/build/tsconfig.json +17 -0
- package/dist/cjs/codeBlockAdvancedPlugin.js +33 -0
- package/dist/cjs/codeBlockAdvancedPluginType.js +5 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/nodeviews/codeBlockAdvanced.js +212 -0
- package/dist/cjs/nodeviews/codeBlockNodeWithToDOMFixed.js +55 -0
- package/dist/cjs/nodeviews/codemirrorSync/syncCMWithPM.js +38 -0
- package/dist/cjs/nodeviews/codemirrorSync/updateCMSelection.js +36 -0
- package/dist/cjs/nodeviews/extensions/bidiCharWarning.js +83 -0
- package/dist/cjs/nodeviews/extensions/keymap/backspace.js +33 -0
- package/dist/cjs/nodeviews/extensions/keymap/index.js +129 -0
- package/dist/cjs/nodeviews/extensions/keymap/maybeEscape.js +63 -0
- package/dist/cjs/nodeviews/languages/languageMap.js +100 -0
- package/dist/cjs/nodeviews/languages/loader.js +50 -0
- package/dist/cjs/nodeviews/lazyCodeBlockAdvanced.js +29 -0
- package/dist/cjs/pm-plugins/main.js +17 -0
- package/dist/cjs/ui/syntaxHighlightingTheme.js +76 -0
- package/dist/cjs/ui/theme.js +55 -0
- package/dist/es2019/codeBlockAdvancedPlugin.js +26 -0
- package/dist/es2019/codeBlockAdvancedPluginType.js +1 -0
- package/dist/es2019/index.js +4 -0
- package/dist/es2019/nodeviews/codeBlockAdvanced.js +170 -0
- package/dist/es2019/nodeviews/codeBlockNodeWithToDOMFixed.js +42 -0
- package/dist/es2019/nodeviews/codemirrorSync/syncCMWithPM.js +35 -0
- package/dist/es2019/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
- package/dist/es2019/nodeviews/extensions/bidiCharWarning.js +53 -0
- package/dist/es2019/nodeviews/extensions/keymap/backspace.js +28 -0
- package/dist/es2019/nodeviews/extensions/keymap/index.js +107 -0
- package/dist/es2019/nodeviews/extensions/keymap/maybeEscape.js +57 -0
- package/dist/es2019/nodeviews/languages/languageMap.js +82 -0
- package/dist/es2019/nodeviews/languages/loader.js +37 -0
- package/dist/es2019/nodeviews/lazyCodeBlockAdvanced.js +18 -0
- package/dist/es2019/pm-plugins/main.js +11 -0
- package/dist/es2019/ui/syntaxHighlightingTheme.js +70 -0
- package/dist/es2019/ui/theme.js +49 -0
- package/dist/esm/codeBlockAdvancedPlugin.js +27 -0
- package/dist/esm/codeBlockAdvancedPluginType.js +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/nodeviews/codeBlockAdvanced.js +205 -0
- package/dist/esm/nodeviews/codeBlockNodeWithToDOMFixed.js +48 -0
- package/dist/esm/nodeviews/codemirrorSync/syncCMWithPM.js +32 -0
- package/dist/esm/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
- package/dist/esm/nodeviews/extensions/bidiCharWarning.js +77 -0
- package/dist/esm/nodeviews/extensions/keymap/backspace.js +27 -0
- package/dist/esm/nodeviews/extensions/keymap/index.js +122 -0
- package/dist/esm/nodeviews/extensions/keymap/maybeEscape.js +56 -0
- package/dist/esm/nodeviews/languages/languageMap.js +86 -0
- package/dist/esm/nodeviews/languages/loader.js +44 -0
- package/dist/esm/nodeviews/lazyCodeBlockAdvanced.js +17 -0
- package/dist/esm/pm-plugins/main.js +11 -0
- package/dist/esm/ui/syntaxHighlightingTheme.js +70 -0
- package/dist/esm/ui/theme.js +49 -0
- package/dist/types/codeBlockAdvancedPlugin.d.ts +2 -0
- package/dist/types/codeBlockAdvancedPluginType.d.ts +11 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/nodeviews/codeBlockAdvanced.d.ts +38 -0
- package/dist/types/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
- package/dist/types/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
- package/dist/types/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
- package/dist/types/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
- package/dist/types/nodeviews/extensions/keymap/backspace.d.ts +12 -0
- package/dist/types/nodeviews/extensions/keymap/index.d.ts +14 -0
- package/dist/types/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
- package/dist/types/nodeviews/languages/languageMap.d.ts +5 -0
- package/dist/types/nodeviews/languages/loader.d.ts +11 -0
- package/dist/types/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
- package/dist/types/pm-plugins/main.d.ts +10 -0
- package/dist/types/ui/syntaxHighlightingTheme.d.ts +2 -0
- package/dist/types/ui/theme.d.ts +1 -0
- package/dist/types-ts4.5/codeBlockAdvancedPlugin.d.ts +2 -0
- package/dist/types-ts4.5/codeBlockAdvancedPluginType.d.ts +15 -0
- package/dist/types-ts4.5/index.d.ts +2 -0
- package/dist/types-ts4.5/nodeviews/codeBlockAdvanced.d.ts +38 -0
- package/dist/types-ts4.5/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
- package/dist/types-ts4.5/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
- package/dist/types-ts4.5/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
- package/dist/types-ts4.5/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/backspace.d.ts +12 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/index.d.ts +14 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
- package/dist/types-ts4.5/nodeviews/languages/languageMap.d.ts +5 -0
- package/dist/types-ts4.5/nodeviews/languages/loader.d.ts +12 -0
- package/dist/types-ts4.5/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +10 -0
- package/dist/types-ts4.5/ui/syntaxHighlightingTheme.d.ts +2 -0
- package/dist/types-ts4.5/ui/theme.d.ts +1 -0
- package/docs/0-intro.tsx +56 -0
- package/package.json +96 -0
- package/src/codeBlockAdvancedPlugin.tsx +25 -0
- package/src/codeBlockAdvancedPluginType.ts +18 -0
- package/src/index.ts +5 -0
- package/src/nodeviews/codeBlockAdvanced.ts +223 -0
- package/src/nodeviews/codeBlockNodeWithToDOMFixed.ts +59 -0
- package/src/nodeviews/codemirrorSync/syncCMWithPM.ts +40 -0
- package/src/nodeviews/codemirrorSync/updateCMSelection.ts +40 -0
- package/src/nodeviews/extensions/bidiCharWarning.ts +72 -0
- package/src/nodeviews/extensions/keymap/backspace.ts +47 -0
- package/src/nodeviews/extensions/keymap/index.ts +118 -0
- package/src/nodeviews/extensions/keymap/maybeEscape.ts +69 -0
- package/src/nodeviews/languages/languageMap.ts +91 -0
- package/src/nodeviews/languages/loader.ts +44 -0
- package/src/nodeviews/lazyCodeBlockAdvanced.ts +32 -0
- package/src/pm-plugins/main.ts +22 -0
- package/src/ui/syntaxHighlightingTheme.ts +50 -0
- package/src/ui/theme.ts +52 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { codeBlock } from '@atlaskit/adf-schema';
|
|
2
|
+
import { CodeBlockSharedCssClassName } from '@atlaskit/editor-common/styles';
|
|
3
|
+
import type { NodeSpec, DOMOutputSpec, Node } from '@atlaskit/editor-prosemirror/model';
|
|
4
|
+
|
|
5
|
+
const codeBlockClassNames = {
|
|
6
|
+
container: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER,
|
|
7
|
+
start: CodeBlockSharedCssClassName.CODEBLOCK_START,
|
|
8
|
+
end: CodeBlockSharedCssClassName.CODEBLOCK_END,
|
|
9
|
+
contentWrapper: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPER,
|
|
10
|
+
contentWrapped: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPED,
|
|
11
|
+
gutter: CodeBlockSharedCssClassName.CODEBLOCK_LINE_NUMBER_GUTTER,
|
|
12
|
+
content: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT,
|
|
13
|
+
lineNumberWidget: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER_LINE_NUMBER_WIDGET,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// From: `packages/editor/editor-plugin-code-block/src/nodeviews/code-block.ts`
|
|
17
|
+
const toDOM = (node: Node, contentEditable: boolean, formattedAriaLabel: string): DOMOutputSpec => [
|
|
18
|
+
'div',
|
|
19
|
+
{ class: codeBlockClassNames.container },
|
|
20
|
+
['div', { class: codeBlockClassNames.start, contenteditable: 'false' }],
|
|
21
|
+
[
|
|
22
|
+
'div',
|
|
23
|
+
{
|
|
24
|
+
class: codeBlockClassNames.contentWrapper,
|
|
25
|
+
},
|
|
26
|
+
[
|
|
27
|
+
'div',
|
|
28
|
+
{
|
|
29
|
+
class: codeBlockClassNames.gutter,
|
|
30
|
+
contenteditable: 'false',
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
'div',
|
|
35
|
+
{
|
|
36
|
+
class: codeBlockClassNames.content,
|
|
37
|
+
},
|
|
38
|
+
[
|
|
39
|
+
'code',
|
|
40
|
+
{
|
|
41
|
+
'data-language': node.attrs.language || '',
|
|
42
|
+
spellcheck: 'false',
|
|
43
|
+
contenteditable: contentEditable ? 'true' : 'false',
|
|
44
|
+
'data-testid': 'code-block--code',
|
|
45
|
+
'aria-label': formattedAriaLabel,
|
|
46
|
+
},
|
|
47
|
+
0,
|
|
48
|
+
],
|
|
49
|
+
],
|
|
50
|
+
],
|
|
51
|
+
['div', { class: codeBlockClassNames.end, contenteditable: 'false' }],
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
export const codeBlockNodeWithFixedToDOM = (): NodeSpec => {
|
|
55
|
+
return {
|
|
56
|
+
...codeBlock,
|
|
57
|
+
toDOM: (node) => toDOM(node, false, ''),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ViewUpdate } from '@codemirror/view';
|
|
2
|
+
|
|
3
|
+
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
|
|
4
|
+
import { EditorView } from '@atlaskit/editor-prosemirror/view';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
view: EditorView;
|
|
8
|
+
update: ViewUpdate;
|
|
9
|
+
offset: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* Synchronises the CodeMirror update changes with the Prosemirror editor
|
|
15
|
+
*
|
|
16
|
+
* @param props.view EditorView - Prosemirror EditorView
|
|
17
|
+
* @param props.update ViewUpdate - CodeMirror ViewUpdate
|
|
18
|
+
* @param props.offset number - position where the code block starts in prosemirror
|
|
19
|
+
*/
|
|
20
|
+
export const syncCMWithPM = ({ view, update, offset }: Props): void => {
|
|
21
|
+
const { main } = update.state.selection;
|
|
22
|
+
const selFrom = offset + main.from;
|
|
23
|
+
const selTo = offset + main.to;
|
|
24
|
+
|
|
25
|
+
const pmSel = view.state.selection;
|
|
26
|
+
if (update.docChanged || pmSel.from !== selFrom || pmSel.to !== selTo) {
|
|
27
|
+
const tr = view.state.tr;
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
29
|
+
update.changes.iterChanges((fromA, toA, fromB, toB, text) => {
|
|
30
|
+
if (text.length) {
|
|
31
|
+
tr.replaceWith(offset + fromA, offset + toA, view.state.schema.text(text.toString()));
|
|
32
|
+
} else {
|
|
33
|
+
tr.delete(offset + fromA, offset + toA);
|
|
34
|
+
}
|
|
35
|
+
offset += toB - fromB - (toA - fromA);
|
|
36
|
+
});
|
|
37
|
+
tr.setSelection(TextSelection.create(tr.doc, selFrom, selTo));
|
|
38
|
+
view.dispatch(tr);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { TransactionSpec as CMTransactionSpec } from '@codemirror/state';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* Compares the updated text with the current to determine the transaction to fire
|
|
6
|
+
* in the CodeMirror editor.
|
|
7
|
+
*
|
|
8
|
+
* @param curText string - the current CodeMirror text
|
|
9
|
+
* @param newText string - the new CodeMirror text
|
|
10
|
+
* @param updateCallback Callback to process the CodeMirror transaction
|
|
11
|
+
*/
|
|
12
|
+
export const updateCMSelection = (
|
|
13
|
+
curText: string,
|
|
14
|
+
newText: string,
|
|
15
|
+
updateCallback: (value: CMTransactionSpec) => void,
|
|
16
|
+
) => {
|
|
17
|
+
if (newText !== curText) {
|
|
18
|
+
let start = 0,
|
|
19
|
+
curEnd = curText.length,
|
|
20
|
+
newEnd = newText.length;
|
|
21
|
+
while (start < curEnd && curText.charCodeAt(start) === newText.charCodeAt(start)) {
|
|
22
|
+
++start;
|
|
23
|
+
}
|
|
24
|
+
while (
|
|
25
|
+
curEnd > start &&
|
|
26
|
+
newEnd > start &&
|
|
27
|
+
curText.charCodeAt(curEnd - 1) === newText.charCodeAt(newEnd - 1)
|
|
28
|
+
) {
|
|
29
|
+
curEnd--;
|
|
30
|
+
newEnd--;
|
|
31
|
+
}
|
|
32
|
+
updateCallback({
|
|
33
|
+
changes: {
|
|
34
|
+
from: start,
|
|
35
|
+
to: curEnd,
|
|
36
|
+
insert: newText.slice(start, newEnd),
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code based on warning from @atlaskit/code
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
EditorView as CodeMirror,
|
|
6
|
+
ViewPlugin,
|
|
7
|
+
ViewUpdate,
|
|
8
|
+
Decoration,
|
|
9
|
+
MatchDecorator,
|
|
10
|
+
DecorationSet,
|
|
11
|
+
WidgetType,
|
|
12
|
+
} from '@codemirror/view';
|
|
13
|
+
|
|
14
|
+
import { token } from '@atlaskit/tokens';
|
|
15
|
+
|
|
16
|
+
const bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/gu;
|
|
17
|
+
|
|
18
|
+
const placeholderMatcher = new MatchDecorator({
|
|
19
|
+
regexp: bidiCharacterRegex,
|
|
20
|
+
decoration: (match) =>
|
|
21
|
+
Decoration.replace({
|
|
22
|
+
widget: new PlaceholderWidget(match[0]),
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const bidiCharWarningExtension = ViewPlugin.fromClass(
|
|
27
|
+
class {
|
|
28
|
+
placeholders: DecorationSet;
|
|
29
|
+
constructor(view: CodeMirror) {
|
|
30
|
+
this.placeholders = placeholderMatcher.createDeco(view);
|
|
31
|
+
}
|
|
32
|
+
update(update: ViewUpdate) {
|
|
33
|
+
this.placeholders = placeholderMatcher.updateDeco(update, this.placeholders);
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
decorations: (instance) => instance.placeholders,
|
|
38
|
+
provide: (plugin) =>
|
|
39
|
+
CodeMirror.atomicRanges.of((view) => {
|
|
40
|
+
return view.plugin(plugin)?.placeholders || Decoration.none;
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
function getBidiCharacterCode(bidiCharacter: string) {
|
|
46
|
+
const bidiCharacterCode = bidiCharacter.codePointAt(0)?.toString(16);
|
|
47
|
+
|
|
48
|
+
return `U+${bidiCharacterCode}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class PlaceholderWidget extends WidgetType {
|
|
52
|
+
constructor(readonly name: string) {
|
|
53
|
+
super();
|
|
54
|
+
}
|
|
55
|
+
eq(other: PlaceholderWidget) {
|
|
56
|
+
return this.name === other.name;
|
|
57
|
+
}
|
|
58
|
+
toDOM() {
|
|
59
|
+
const elt = document.createElement('span');
|
|
60
|
+
elt.setAttribute('data-bidi-character-code', this.name);
|
|
61
|
+
elt.style.cssText = `
|
|
62
|
+
padding: 0 3px;
|
|
63
|
+
color: ${token('color.text.warning')};
|
|
64
|
+
background: ${token('color.background.warning')};
|
|
65
|
+
aria-hidden="true"`;
|
|
66
|
+
elt.textContent = `<${getBidiCharacterCode(this.name)}>`;
|
|
67
|
+
return elt;
|
|
68
|
+
}
|
|
69
|
+
ignoreEvent() {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { EditorView as CodeMirror } from '@codemirror/view';
|
|
2
|
+
|
|
3
|
+
import type { getPosHandlerNode } from '@atlaskit/editor-common/types';
|
|
4
|
+
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
5
|
+
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
|
|
6
|
+
import { EditorView } from '@atlaskit/editor-prosemirror/view';
|
|
7
|
+
|
|
8
|
+
interface BackspaceProps {
|
|
9
|
+
view: EditorView;
|
|
10
|
+
cm: CodeMirror;
|
|
11
|
+
getPos: getPosHandlerNode;
|
|
12
|
+
getNode: () => PMNode;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const backspaceKeymap = ({ cm, view, getPos, getNode }: BackspaceProps) => {
|
|
16
|
+
const ranges = cm.state.selection.ranges;
|
|
17
|
+
|
|
18
|
+
if (ranges.length > 1) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const selection = ranges[0];
|
|
23
|
+
|
|
24
|
+
if (selection && (!selection.empty || selection.anchor > 0)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (cm.state.doc.lines >= 2) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const state = view.state;
|
|
33
|
+
const pos = getPos() ?? 0;
|
|
34
|
+
const node = getNode();
|
|
35
|
+
|
|
36
|
+
const tr = state.tr.replaceWith(
|
|
37
|
+
pos,
|
|
38
|
+
pos + node.nodeSize,
|
|
39
|
+
state.schema.nodes.paragraph.createChecked({}, node.content),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos)));
|
|
43
|
+
|
|
44
|
+
view.dispatch(tr);
|
|
45
|
+
view.focus();
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
|
|
2
|
+
import { Extension } from '@codemirror/state';
|
|
3
|
+
import { KeyBinding, keymap as cmKeymap } from '@codemirror/view';
|
|
4
|
+
|
|
5
|
+
import { RelativeSelectionPos } from '@atlaskit/editor-common/selection';
|
|
6
|
+
import type { getPosHandlerNode } from '@atlaskit/editor-common/types';
|
|
7
|
+
import { exitCode } from '@atlaskit/editor-prosemirror/commands';
|
|
8
|
+
import { undo, redo } from '@atlaskit/editor-prosemirror/history';
|
|
9
|
+
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
10
|
+
import { EditorView } from '@atlaskit/editor-prosemirror/view';
|
|
11
|
+
|
|
12
|
+
import { backspaceKeymap } from './backspace';
|
|
13
|
+
import { maybeEscapeKeymap } from './maybeEscape';
|
|
14
|
+
|
|
15
|
+
interface KeymapProps {
|
|
16
|
+
view: EditorView;
|
|
17
|
+
getNode: () => PMNode;
|
|
18
|
+
getPos: getPosHandlerNode;
|
|
19
|
+
selectCodeBlockNode: (relativeSelectionPos: RelativeSelectionPos | undefined) => void;
|
|
20
|
+
onMaybeNodeSelection: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const keymapExtension = ({
|
|
24
|
+
view,
|
|
25
|
+
getNode,
|
|
26
|
+
getPos,
|
|
27
|
+
selectCodeBlockNode,
|
|
28
|
+
onMaybeNodeSelection,
|
|
29
|
+
}: KeymapProps): Extension => {
|
|
30
|
+
return cmKeymap.of(
|
|
31
|
+
codeBlockKeymap({ view, getNode, getPos, selectCodeBlockNode, onMaybeNodeSelection }),
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const codeBlockKeymap = ({
|
|
36
|
+
view,
|
|
37
|
+
getNode,
|
|
38
|
+
getPos,
|
|
39
|
+
selectCodeBlockNode,
|
|
40
|
+
onMaybeNodeSelection,
|
|
41
|
+
}: KeymapProps): readonly KeyBinding[] => {
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
key: 'ArrowUp',
|
|
45
|
+
run: (cm) =>
|
|
46
|
+
maybeEscapeKeymap({
|
|
47
|
+
unit: 'line',
|
|
48
|
+
dir: -1,
|
|
49
|
+
cm,
|
|
50
|
+
view,
|
|
51
|
+
getNode,
|
|
52
|
+
getPos,
|
|
53
|
+
selectCodeBlockNode,
|
|
54
|
+
onMaybeNodeSelection,
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
key: 'ArrowLeft',
|
|
59
|
+
run: (cm) =>
|
|
60
|
+
maybeEscapeKeymap({
|
|
61
|
+
unit: 'char',
|
|
62
|
+
dir: -1,
|
|
63
|
+
cm,
|
|
64
|
+
view,
|
|
65
|
+
getNode,
|
|
66
|
+
getPos,
|
|
67
|
+
selectCodeBlockNode,
|
|
68
|
+
onMaybeNodeSelection,
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key: 'ArrowDown',
|
|
73
|
+
run: (cm) =>
|
|
74
|
+
maybeEscapeKeymap({
|
|
75
|
+
unit: 'line',
|
|
76
|
+
dir: 1,
|
|
77
|
+
cm,
|
|
78
|
+
view,
|
|
79
|
+
getNode,
|
|
80
|
+
getPos,
|
|
81
|
+
selectCodeBlockNode,
|
|
82
|
+
onMaybeNodeSelection,
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
key: 'ArrowRight',
|
|
87
|
+
run: (cm) =>
|
|
88
|
+
maybeEscapeKeymap({
|
|
89
|
+
unit: 'char',
|
|
90
|
+
dir: 1,
|
|
91
|
+
cm,
|
|
92
|
+
view,
|
|
93
|
+
getNode,
|
|
94
|
+
getPos,
|
|
95
|
+
selectCodeBlockNode,
|
|
96
|
+
onMaybeNodeSelection,
|
|
97
|
+
}),
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
key: 'Ctrl-Enter',
|
|
101
|
+
run: () => {
|
|
102
|
+
if (!exitCode(view.state, view.dispatch)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
view.focus();
|
|
106
|
+
return true;
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
{ key: 'Ctrl-z', mac: 'Cmd-z', run: () => undo(view.state, view.dispatch) },
|
|
110
|
+
{ key: 'Shift-Ctrl-z', mac: 'Shift-Cmd-z', run: () => redo(view.state, view.dispatch) },
|
|
111
|
+
{ key: 'Ctrl-y', mac: 'Cmd-y', run: () => redo(view.state, view.dispatch) },
|
|
112
|
+
{
|
|
113
|
+
key: 'Backspace',
|
|
114
|
+
run: (cm) => backspaceKeymap({ cm, view, getNode, getPos }),
|
|
115
|
+
},
|
|
116
|
+
...defaultKeymap.concat(indentWithTab),
|
|
117
|
+
];
|
|
118
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EditorView as CodeMirror } from '@codemirror/view';
|
|
2
|
+
|
|
3
|
+
import { RelativeSelectionPos } from '@atlaskit/editor-common/selection';
|
|
4
|
+
import type { getPosHandlerNode } from '@atlaskit/editor-common/types';
|
|
5
|
+
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
6
|
+
import { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
7
|
+
import { EditorView } from '@atlaskit/editor-prosemirror/view';
|
|
8
|
+
|
|
9
|
+
interface MaybeEscapeProps {
|
|
10
|
+
unit: 'line' | 'char';
|
|
11
|
+
dir: -1 | 1;
|
|
12
|
+
view: EditorView;
|
|
13
|
+
cm: CodeMirror;
|
|
14
|
+
getPos: getPosHandlerNode;
|
|
15
|
+
getNode: () => PMNode;
|
|
16
|
+
onMaybeNodeSelection: () => void;
|
|
17
|
+
selectCodeBlockNode: (relativeSelectionPos: RelativeSelectionPos | undefined) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const maybeEscapeKeymap = ({
|
|
21
|
+
unit,
|
|
22
|
+
dir,
|
|
23
|
+
view,
|
|
24
|
+
cm,
|
|
25
|
+
getPos,
|
|
26
|
+
getNode,
|
|
27
|
+
onMaybeNodeSelection,
|
|
28
|
+
selectCodeBlockNode,
|
|
29
|
+
}: MaybeEscapeProps): boolean => {
|
|
30
|
+
if (unit === 'char') {
|
|
31
|
+
onMaybeNodeSelection();
|
|
32
|
+
}
|
|
33
|
+
const node = getNode();
|
|
34
|
+
const { state } = cm;
|
|
35
|
+
let main: { from: number; to: number; empty: boolean; head: number } = state.selection.main;
|
|
36
|
+
if (!main.empty) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (unit === 'line') {
|
|
40
|
+
main = { ...state.doc.lineAt(main.head), head: 0, empty: false };
|
|
41
|
+
}
|
|
42
|
+
if (dir < 0 ? main.from > 0 : main.to < state.doc.length) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const targetPos = (getPos?.() ?? 0) + (dir < 0 ? 0 : node.nodeSize);
|
|
46
|
+
if (unit === 'char') {
|
|
47
|
+
view.focus();
|
|
48
|
+
selectCodeBlockNode(dir === -1 ? RelativeSelectionPos.Start : RelativeSelectionPos.End);
|
|
49
|
+
return true;
|
|
50
|
+
} else {
|
|
51
|
+
createParagraphIfEndOfDoc(view, targetPos);
|
|
52
|
+
const tr = view.state.tr;
|
|
53
|
+
const selection = Selection.near(tr.doc.resolve(targetPos), dir);
|
|
54
|
+
|
|
55
|
+
tr.setSelection(selection).scrollIntoView();
|
|
56
|
+
view.dispatch(tr);
|
|
57
|
+
view.focus();
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const createParagraphIfEndOfDoc = (view: EditorView, targetPos: number) => {
|
|
63
|
+
if (targetPos === view.state.doc.content.size) {
|
|
64
|
+
const paragraph = view.state.schema.nodes.paragraph.createChecked({});
|
|
65
|
+
const tr = view.state.tr.insert(targetPos, paragraph);
|
|
66
|
+
// Note: we purposefully do a multi-dispatch here, otherwise we get stuck in codemirror
|
|
67
|
+
view.dispatch(tr);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { LanguageDescription } from '@codemirror/language';
|
|
2
|
+
import { languages } from '@codemirror/language-data';
|
|
3
|
+
|
|
4
|
+
import type { LanguageAlias } from '@atlaskit/code';
|
|
5
|
+
|
|
6
|
+
type LanguageAliasValue = LanguageAlias[0];
|
|
7
|
+
|
|
8
|
+
// getLanguageIdentifier defines `language.alias[0]`
|
|
9
|
+
export const mapLanguageToCodeMirror = (language: LanguageAliasValue) => {
|
|
10
|
+
if (!language || language === 'none') {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
switch (language) {
|
|
14
|
+
case 'coldfusion':
|
|
15
|
+
return languages.find((l) => {
|
|
16
|
+
return l.name.toLowerCase() === 'xml';
|
|
17
|
+
});
|
|
18
|
+
case 'matlab':
|
|
19
|
+
return languages.find((l) => {
|
|
20
|
+
return l.name.toLowerCase() === 'octave';
|
|
21
|
+
});
|
|
22
|
+
case 'javafx':
|
|
23
|
+
return languages.find((l) => {
|
|
24
|
+
return l.name.toLowerCase() === 'java';
|
|
25
|
+
});
|
|
26
|
+
case 'vbnet':
|
|
27
|
+
return languages.find((l) => {
|
|
28
|
+
return l.name === 'VB.NET';
|
|
29
|
+
});
|
|
30
|
+
case 'pas':
|
|
31
|
+
return languages.find((l) => {
|
|
32
|
+
return l.name.toLowerCase() === 'pascal';
|
|
33
|
+
});
|
|
34
|
+
case 'cuda':
|
|
35
|
+
return languages.find((l) => {
|
|
36
|
+
return l.name.toLowerCase() === 'c++';
|
|
37
|
+
});
|
|
38
|
+
case 'racket':
|
|
39
|
+
return languages.find((l) => {
|
|
40
|
+
return l.name === 'Common Lisp';
|
|
41
|
+
});
|
|
42
|
+
case 'abap':
|
|
43
|
+
return languages.find((l) => {
|
|
44
|
+
return l.name === 'SQL';
|
|
45
|
+
});
|
|
46
|
+
case 'standardmL':
|
|
47
|
+
return languages.find((l) => {
|
|
48
|
+
return l.name === 'SML';
|
|
49
|
+
});
|
|
50
|
+
case 'objective-j':
|
|
51
|
+
return languages.find((l) => {
|
|
52
|
+
return l.name === 'Objective-C';
|
|
53
|
+
});
|
|
54
|
+
case 'docker':
|
|
55
|
+
return languages.find((l) => {
|
|
56
|
+
return l.name === 'Dockerfile';
|
|
57
|
+
});
|
|
58
|
+
case 'arduino':
|
|
59
|
+
return languages.find((l) => {
|
|
60
|
+
return l.name === 'C++';
|
|
61
|
+
});
|
|
62
|
+
case 'visualbasic':
|
|
63
|
+
return languages.find((l) => {
|
|
64
|
+
return l.name === 'VB.NET';
|
|
65
|
+
});
|
|
66
|
+
case 'elixir':
|
|
67
|
+
return LanguageDescription.of({
|
|
68
|
+
name: 'Elixir',
|
|
69
|
+
load() {
|
|
70
|
+
return import(
|
|
71
|
+
/* webpackChunkName: "@atlaskit-internal_@atlaskit/editor-plugin-code-block-advanced-lang-elixir" */
|
|
72
|
+
'codemirror-lang-elixir'
|
|
73
|
+
).then((m) => m.elixir());
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
case 'graphql':
|
|
77
|
+
return LanguageDescription.of({
|
|
78
|
+
name: 'GraphQL',
|
|
79
|
+
load() {
|
|
80
|
+
return import(
|
|
81
|
+
/* webpackChunkName: "@atlaskit-internal_@atlaskit/editor-plugin-code-block-advanced-lang-graphql" */
|
|
82
|
+
'cm6-graphql'
|
|
83
|
+
).then((m) => m.graphqlLanguageSupport());
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
default:
|
|
87
|
+
return languages.find((l) => {
|
|
88
|
+
return l.alias.includes(language) || l.name.toLowerCase() === language?.toLowerCase();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { LanguageSupport } from '@codemirror/language';
|
|
2
|
+
|
|
3
|
+
import { mapLanguageToCodeMirror } from './languageMap';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Manages loading the languages (for syntax highlighting, etc.)
|
|
7
|
+
* from CodeMirror and updating the language in the CodeMirror view
|
|
8
|
+
*/
|
|
9
|
+
export class LanguageLoader {
|
|
10
|
+
private languageName: string = '';
|
|
11
|
+
|
|
12
|
+
constructor(private updateLanguageCompartment: (value: LanguageSupport | []) => void) {}
|
|
13
|
+
|
|
14
|
+
updateLanguage(languageName: string) {
|
|
15
|
+
if (languageName === this.languageName) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const language = mapLanguageToCodeMirror(languageName);
|
|
19
|
+
|
|
20
|
+
const configureEmpty = () => {
|
|
21
|
+
this.updateLanguageCompartment([]);
|
|
22
|
+
this.languageName = '';
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (!language) {
|
|
26
|
+
configureEmpty();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
language
|
|
31
|
+
.load()
|
|
32
|
+
.then((lang) => {
|
|
33
|
+
if (lang) {
|
|
34
|
+
this.updateLanguageCompartment(lang);
|
|
35
|
+
this.languageName = languageName;
|
|
36
|
+
} else {
|
|
37
|
+
configureEmpty();
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.catch(() => {
|
|
41
|
+
configureEmpty();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Extension } from '@codemirror/state';
|
|
2
|
+
|
|
3
|
+
import { withLazyLoading } from '@atlaskit/editor-common/lazy-node-view';
|
|
4
|
+
import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
|
|
5
|
+
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
6
|
+
import type { EditorView } from '@atlaskit/editor-prosemirror/view';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
import type { CodeBlockAdvancedPlugin } from '../codeBlockAdvancedPluginType';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
api: ExtractInjectionAPI<CodeBlockAdvancedPlugin> | undefined;
|
|
13
|
+
extensions: Extension[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const lazyCodeBlockView = (props: Props) => {
|
|
17
|
+
return withLazyLoading({
|
|
18
|
+
nodeName: 'codeBlock',
|
|
19
|
+
getNodeViewOptions: () => {},
|
|
20
|
+
loader: () => {
|
|
21
|
+
const result = import(
|
|
22
|
+
/* webpackChunkName: "@atlaskit-internal_editor-plugin-code-block-advanced-nodeview" */
|
|
23
|
+
'./codeBlockAdvanced'
|
|
24
|
+
).then(({ getCodeBlockAdvancedNodeView }) => {
|
|
25
|
+
return (node: PMNode, view: EditorView, getPos: () => number | undefined) => {
|
|
26
|
+
return getCodeBlockAdvancedNodeView(props)(node, view, getPos);
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
return result;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Extension } from '@codemirror/state';
|
|
2
|
+
|
|
3
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
4
|
+
import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
|
|
5
|
+
|
|
6
|
+
import type { CodeBlockAdvancedPlugin } from '../codeBlockAdvancedPluginType';
|
|
7
|
+
import { lazyCodeBlockView } from '../nodeviews/lazyCodeBlockAdvanced';
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
api: ExtractInjectionAPI<CodeBlockAdvancedPlugin> | undefined;
|
|
11
|
+
extensions: Extension[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const createPlugin = (props: Props) => {
|
|
15
|
+
return new SafePlugin({
|
|
16
|
+
props: {
|
|
17
|
+
nodeViews: {
|
|
18
|
+
codeBlock: lazyCodeBlockView(props),
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HighlightStyle } from '@codemirror/language';
|
|
2
|
+
import { tags } from '@lezer/highlight';
|
|
3
|
+
|
|
4
|
+
import { token } from '@atlaskit/tokens';
|
|
5
|
+
|
|
6
|
+
// Based on `platform/packages/design-system/code/src/internal/theme/styles.tsx`
|
|
7
|
+
export const highlightStyle = HighlightStyle.define([
|
|
8
|
+
{ tag: tags.meta, color: token('color.text') },
|
|
9
|
+
{ tag: tags.link, textDecoration: 'underline' },
|
|
10
|
+
{
|
|
11
|
+
tag: tags.heading,
|
|
12
|
+
textDecoration: 'underline',
|
|
13
|
+
// Custom syntax styling to match existing styling
|
|
14
|
+
// eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
|
|
15
|
+
fontWeight: token('font.weight.bold'),
|
|
16
|
+
},
|
|
17
|
+
{ tag: tags.emphasis, fontStyle: 'italic' },
|
|
18
|
+
{
|
|
19
|
+
tag: tags.strong,
|
|
20
|
+
// Custom syntax styling to match existing styling
|
|
21
|
+
// eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
|
|
22
|
+
fontWeight: token('font.weight.bold'),
|
|
23
|
+
},
|
|
24
|
+
{ tag: tags.strikethrough, textDecoration: 'line-through' },
|
|
25
|
+
{
|
|
26
|
+
tag: tags.keyword,
|
|
27
|
+
color: token('color.text.accent.blue'),
|
|
28
|
+
// Custom syntax styling to match existing styling
|
|
29
|
+
// eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
|
|
30
|
+
fontWeight: token('font.weight.bold'),
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
tag: [tags.atom, tags.bool, tags.url, tags.contentSeparator, tags.labelName],
|
|
34
|
+
color: token('color.text.accent.blue'),
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
tag: [tags.literal, tags.inserted],
|
|
38
|
+
color: token('color.text.accent.blue'),
|
|
39
|
+
},
|
|
40
|
+
{ tag: [tags.string, tags.deleted], color: token('color.text.accent.green') },
|
|
41
|
+
{ tag: [tags.regexp, tags.escape, tags.special(tags.string)], color: token('color.text') },
|
|
42
|
+
{ tag: tags.definition(tags.variableName), color: token('color.text') },
|
|
43
|
+
{ tag: tags.local(tags.variableName), color: token('color.text') },
|
|
44
|
+
{ tag: [tags.typeName, tags.namespace], color: token('color.text.accent.blue') },
|
|
45
|
+
{ tag: tags.className, color: token('color.text.accent.purple') },
|
|
46
|
+
{ tag: [tags.special(tags.variableName), tags.macroName], color: token('color.text') },
|
|
47
|
+
{ tag: tags.definition(tags.propertyName), color: token('color.text') },
|
|
48
|
+
{ tag: tags.comment, color: token('color.text.subtlest'), fontStyle: 'italic' },
|
|
49
|
+
{ tag: tags.invalid, color: token('color.text') },
|
|
50
|
+
]);
|