@difizen/libro-codemirror 0.0.2-alpha.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/LICENSE +21 -0
- package/README.md +0 -0
- package/es/auto-complete/closebrackets.d.ts +12 -0
- package/es/auto-complete/closebrackets.d.ts.map +1 -0
- package/es/auto-complete/closebrackets.js +408 -0
- package/es/auto-complete/completion.d.ts +57 -0
- package/es/auto-complete/completion.d.ts.map +1 -0
- package/es/auto-complete/completion.js +265 -0
- package/es/auto-complete/config.d.ts +22 -0
- package/es/auto-complete/config.d.ts.map +1 -0
- package/es/auto-complete/config.js +44 -0
- package/es/auto-complete/filter.d.ts +13 -0
- package/es/auto-complete/filter.d.ts.map +1 -0
- package/es/auto-complete/filter.js +191 -0
- package/es/auto-complete/index.d.ts +17 -0
- package/es/auto-complete/index.d.ts.map +1 -0
- package/es/auto-complete/index.js +107 -0
- package/es/auto-complete/snippet.d.ts +14 -0
- package/es/auto-complete/snippet.d.ts.map +1 -0
- package/es/auto-complete/snippet.js +447 -0
- package/es/auto-complete/state.d.ts +63 -0
- package/es/auto-complete/state.d.ts.map +1 -0
- package/es/auto-complete/state.js +452 -0
- package/es/auto-complete/theme.d.ts +6 -0
- package/es/auto-complete/theme.d.ts.map +1 -0
- package/es/auto-complete/theme.js +151 -0
- package/es/auto-complete/tooltip.d.ts +5 -0
- package/es/auto-complete/tooltip.d.ts.map +1 -0
- package/es/auto-complete/tooltip.js +365 -0
- package/es/auto-complete/view.d.ts +43 -0
- package/es/auto-complete/view.d.ts.map +1 -0
- package/es/auto-complete/view.js +372 -0
- package/es/auto-complete/word.d.ts +3 -0
- package/es/auto-complete/word.d.ts.map +1 -0
- package/es/auto-complete/word.js +119 -0
- package/es/completion.d.ts +6 -0
- package/es/completion.d.ts.map +1 -0
- package/es/completion.js +84 -0
- package/es/config.d.ts +184 -0
- package/es/config.d.ts.map +1 -0
- package/es/config.js +473 -0
- package/es/editor.d.ts +361 -0
- package/es/editor.d.ts.map +1 -0
- package/es/editor.js +1126 -0
- package/es/factory.d.ts +3 -0
- package/es/factory.d.ts.map +1 -0
- package/es/factory.js +12 -0
- package/es/hyperlink.d.ts +15 -0
- package/es/hyperlink.d.ts.map +1 -0
- package/es/hyperlink.js +120 -0
- package/es/indent.d.ts +8 -0
- package/es/indent.d.ts.map +1 -0
- package/es/indent.js +58 -0
- package/es/indentation-markers/config.d.ts +17 -0
- package/es/indentation-markers/config.d.ts.map +1 -0
- package/es/indentation-markers/config.js +10 -0
- package/es/indentation-markers/index.d.ts +3 -0
- package/es/indentation-markers/index.d.ts.map +1 -0
- package/es/indentation-markers/index.js +160 -0
- package/es/indentation-markers/map.d.ts +77 -0
- package/es/indentation-markers/map.d.ts.map +1 -0
- package/es/indentation-markers/map.js +265 -0
- package/es/indentation-markers/utils.d.ts +27 -0
- package/es/indentation-markers/utils.d.ts.map +1 -0
- package/es/indentation-markers/utils.js +91 -0
- package/es/index.d.ts +11 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +10 -0
- package/es/libro-icon.d.ts +3 -0
- package/es/libro-icon.d.ts.map +1 -0
- package/es/libro-icon.js +2 -0
- package/es/mimetype.d.ts +22 -0
- package/es/mimetype.d.ts.map +1 -0
- package/es/mimetype.js +59 -0
- package/es/mode.d.ts +86 -0
- package/es/mode.d.ts.map +1 -0
- package/es/mode.js +284 -0
- package/es/monitor.d.ts +32 -0
- package/es/monitor.d.ts.map +1 -0
- package/es/monitor.js +129 -0
- package/es/python-lang.d.ts +3 -0
- package/es/python-lang.d.ts.map +1 -0
- package/es/python-lang.js +7 -0
- package/es/style/base.css +131 -0
- package/es/style/theme.css +12 -0
- package/es/style/variables.css +403 -0
- package/es/theme.d.ts +35 -0
- package/es/theme.d.ts.map +1 -0
- package/es/theme.js +225 -0
- package/es/tooltip.d.ts +10 -0
- package/es/tooltip.d.ts.map +1 -0
- package/es/tooltip.js +170 -0
- package/package.json +74 -0
- package/src/auto-complete/README.md +71 -0
- package/src/auto-complete/closebrackets.ts +423 -0
- package/src/auto-complete/completion.ts +345 -0
- package/src/auto-complete/config.ts +101 -0
- package/src/auto-complete/filter.ts +215 -0
- package/src/auto-complete/index.ts +112 -0
- package/src/auto-complete/snippet.ts +394 -0
- package/src/auto-complete/state.ts +472 -0
- package/src/auto-complete/theme.ts +126 -0
- package/src/auto-complete/tooltip.ts +386 -0
- package/src/auto-complete/view.ts +343 -0
- package/src/auto-complete/word.ts +118 -0
- package/src/completion.ts +61 -0
- package/src/config.ts +689 -0
- package/src/editor.ts +1078 -0
- package/src/factory.ts +10 -0
- package/src/hyperlink.ts +95 -0
- package/src/indent.ts +69 -0
- package/src/indentation-markers/config.ts +31 -0
- package/src/indentation-markers/index.ts +192 -0
- package/src/indentation-markers/map.ts +273 -0
- package/src/indentation-markers/utils.ts +84 -0
- package/src/index.ts +11 -0
- package/src/libro-icon.ts +4 -0
- package/src/mimetype.ts +49 -0
- package/src/mode.ts +269 -0
- package/src/monitor.ts +105 -0
- package/src/python-lang.ts +7 -0
- package/src/style/base.css +129 -0
- package/src/style/theme.css +12 -0
- package/src/style/variables.css +405 -0
- package/src/theme.ts +231 -0
- package/src/tooltip.ts +145 -0
package/src/tooltip.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-parameter-properties */
|
|
2
|
+
/* eslint-disable @typescript-eslint/parameter-properties */
|
|
3
|
+
import { StateEffect, StateField } from '@codemirror/state';
|
|
4
|
+
import type { EditorState } from '@codemirror/state';
|
|
5
|
+
import type {
|
|
6
|
+
Command,
|
|
7
|
+
KeyBinding,
|
|
8
|
+
PluginValue,
|
|
9
|
+
Tooltip,
|
|
10
|
+
ViewUpdate,
|
|
11
|
+
} from '@codemirror/view';
|
|
12
|
+
import { ViewPlugin, showTooltip, EditorView } from '@codemirror/view';
|
|
13
|
+
import type { TooltipProvider } from '@difizen/libro-code-editor';
|
|
14
|
+
import { defaultSanitizer } from '@difizen/libro-common';
|
|
15
|
+
import { renderText } from '@difizen/libro-rendermime';
|
|
16
|
+
|
|
17
|
+
export const startTooltipEffect = StateEffect.define<boolean>();
|
|
18
|
+
export const closeTooltipEffect = StateEffect.define<null>();
|
|
19
|
+
const tooltipResultEffect = StateEffect.define<{ tooltipText: string | null }>({});
|
|
20
|
+
|
|
21
|
+
const getCursorTooltips = (state: EditorState, text: string): Tooltip => {
|
|
22
|
+
return {
|
|
23
|
+
pos: state.selection.main.head,
|
|
24
|
+
above: false,
|
|
25
|
+
strictSide: false,
|
|
26
|
+
arrow: false,
|
|
27
|
+
create: () => {
|
|
28
|
+
const dom = document.createElement('div');
|
|
29
|
+
dom.className = 'cm-tooltip-libro';
|
|
30
|
+
renderText({
|
|
31
|
+
sanitizer: defaultSanitizer,
|
|
32
|
+
source: text,
|
|
33
|
+
host: dom,
|
|
34
|
+
mimeType: '',
|
|
35
|
+
});
|
|
36
|
+
return { dom };
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const tooltipField = StateField.define<Tooltip | null>({
|
|
42
|
+
create() {
|
|
43
|
+
return null;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
update(_tooltips, tr) {
|
|
47
|
+
const { effects } = tr;
|
|
48
|
+
for (const effect of effects) {
|
|
49
|
+
if (effect.is(closeTooltipEffect)) {
|
|
50
|
+
return null;
|
|
51
|
+
} else if (effect.is(tooltipResultEffect)) {
|
|
52
|
+
const text = effect.value.tooltipText;
|
|
53
|
+
if (text !== null) {
|
|
54
|
+
return getCursorTooltips(tr.state, text);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
provide: (f) =>
|
|
62
|
+
showTooltip.compute([f], (state) => {
|
|
63
|
+
const filed = state.field(f);
|
|
64
|
+
if (filed === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return filed;
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/// Explicitly start tooltip.
|
|
72
|
+
export const startTooltip: Command = (view: EditorView) => {
|
|
73
|
+
view.dispatch({ effects: startTooltipEffect.of(true) });
|
|
74
|
+
return true;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/// Close tooltip.
|
|
78
|
+
export const closeTooltip: Command = (view: EditorView) => {
|
|
79
|
+
view.dispatch({ effects: closeTooltipEffect.of(null) });
|
|
80
|
+
return true;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const tooltipKeymap: readonly KeyBinding[] = [
|
|
84
|
+
{ key: 'Shift-Tab', run: startTooltip },
|
|
85
|
+
{ key: 'Escape', run: closeTooltip },
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
const cursorTooltipBaseTheme = EditorView.baseTheme({
|
|
89
|
+
'.cm-tooltip.cm-tooltip-libro': {
|
|
90
|
+
zIndex: '10001',
|
|
91
|
+
color: 'rgba(0,0,0,0.87)',
|
|
92
|
+
padding: '2px 7px',
|
|
93
|
+
borderRadius: '4px',
|
|
94
|
+
boxShadow:
|
|
95
|
+
'rgba(0,0,0,0.2) 0px 3px 5px -1px, rgba(0,0,0,0.0.14) 0px 6px 10px 0px, rgba(0,0,0,0.0.12) 0px 1px 18px 0px',
|
|
96
|
+
maxHeight: '350px',
|
|
97
|
+
overflow: 'auto',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
class TooltipPlugin implements PluginValue {
|
|
102
|
+
constructor(
|
|
103
|
+
readonly view: EditorView,
|
|
104
|
+
readonly tooltipProvider: TooltipProvider | undefined,
|
|
105
|
+
) {}
|
|
106
|
+
|
|
107
|
+
update(update: ViewUpdate) {
|
|
108
|
+
update.transactions.forEach((tr) => {
|
|
109
|
+
for (const effect of tr.effects) {
|
|
110
|
+
if (effect.is(startTooltipEffect) && this.tooltipProvider) {
|
|
111
|
+
this.tooltipProvider({
|
|
112
|
+
cursorPosition: update.view.state.selection.main.anchor,
|
|
113
|
+
})
|
|
114
|
+
.then((result) => {
|
|
115
|
+
update.view.dispatch({
|
|
116
|
+
effects: tooltipResultEffect.of({
|
|
117
|
+
tooltipText: result,
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
return undefined;
|
|
121
|
+
})
|
|
122
|
+
.catch(() => {
|
|
123
|
+
//
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const asyncShowTooltip = (tooltipProvider: TooltipProvider | undefined) =>
|
|
132
|
+
ViewPlugin.define((view) => new TooltipPlugin(view, tooltipProvider), {
|
|
133
|
+
eventHandlers: {
|
|
134
|
+
blur() {
|
|
135
|
+
const state = this.view.state.field(tooltipField, false);
|
|
136
|
+
if (state) {
|
|
137
|
+
this.view.dispatch({ effects: closeTooltipEffect.of(null) });
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
export function tabTooltip(tooltipProvider: TooltipProvider | undefined) {
|
|
144
|
+
return [tooltipField, cursorTooltipBaseTheme, asyncShowTooltip(tooltipProvider)];
|
|
145
|
+
}
|