-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
80 lines (71 loc) · 1.93 KB
/
main.ts
File metadata and controls
80 lines (71 loc) · 1.93 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
import { EditorView } from '@codemirror/view';
import { Prec } from '@codemirror/state';
import { vim, Vim } from '@replit/codemirror-vim';
import { MarkEdit } from 'markedit-api';
const theme = EditorView.baseTheme({
'.cm-vim-panel': {
paddingTop: '3px',
paddingBottom: '3px',
},
'.cm-vim-panel *': {
fontFamily: 'monospace',
fontSize: '14px',
},
});
MarkEdit.addExtension([
theme,
Prec.highest(vim({ status: true })),
]);
/**
* Apply custom key mappings from an array.
*
* Example:
* [
* { "before": "jj", "after": "<Esc>", "mode": "insert" },
* { "before": "Y", "after": "y$" }
* ]
*/
const applyMappings = (mappings: any) => {
if (Array.isArray(mappings)) {
mappings.forEach(mapping => {
const { before, after, mode } = mapping;
if (typeof before === 'string' && typeof after === 'string') {
Vim.map(before, after, mode);
}
});
}
};
(async () => {
// From userSettings (settings.json)
applyMappings(MarkEdit.userSettings['extension.markeditVim']?.mappings);
// From markedit-vim.json
const documents = MarkEdit.getDirectoryPath('documents');
const configPath = `${documents}/scripts/markedit-vim.json`;
const content = await MarkEdit.getFileContent(configPath);
if (content) {
try {
const config = JSON.parse(content);
applyMappings(config.mappings);
} catch (e) {
console.error('Failed to parse markedit-vim.json', e);
}
}
})();
// Work around a Safari bug where status label is duplicate
MarkEdit.onEditorReady(({ dom }) => {
const panel = dom.querySelector('.cm-panels-bottom');
if (panel === null) {
return;
}
const observer = new MutationObserver(() => {
const span = Array.from(panel.querySelectorAll('span')).find(span => span.style.top === '1px');
if (span) {
span.style.display = 'none';
}
});
observer.observe(panel, {
attributes: true,
childList: true,
subtree: true,
});
});