forked from jupyterlab-contrib/spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
176 lines (146 loc) · 5.44 KB
/
index.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
JupyterFrontEnd, JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IEditorTracker
} from '@jupyterlab/fileeditor';
import {
INotebookTracker
} from '@jupyterlab/notebook';
import {
ICommandPalette
} from '@jupyterlab/apputils';
import '../style/index.css';
import * as CodeMirror from 'codemirror';
declare function require(name:string): any;
let Typo = require("typo-js");
/**
* SpellChecker
*/
class SpellChecker {
dictionary: any;
dict_promise: any;
app: JupyterFrontEnd;
tracker: INotebookTracker;
palette: ICommandPalette;
editor_tracker: IEditorTracker;
// Default Options
check_spelling: boolean = true;
aff_url: string = 'https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.aff';
dict_url: string = 'https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.dic';
lang_code: string = "en_Us";
rx_word_char: RegExp = /[^-\[\]{}():\/!;&@$£%§<>"*+=?.,~\\^|_`#±\s\t]/;
rx_non_word_char: RegExp = /[-\[\]{}():\/!;&@$£%§<>"*+=?.,~\\^|_`#±\s\t]/;
accepted_types = [
'text/plain',
'text/x-ipythongfm', // IPython GFM = GitHub Flavored Markdown, applies to all .md files
];
constructor(app: JupyterFrontEnd, notebook_tracker: INotebookTracker, palette: ICommandPalette, editor_tracker: IEditorTracker){
this.app = app;
this.tracker = notebook_tracker;
this.editor_tracker = editor_tracker;
this.palette = palette;
this.setup_button();
this.load_dictionary();
this.tracker.activeCellChanged.connect(this.onActiveCellChanged, this);
this.editor_tracker.widgetAdded.connect((sender, widget) => {
let file_editor = widget.content;
if (this.accepted_types.indexOf(file_editor.model.mimeType) !== -1) {
let editor = this.extract_editor(file_editor);
this.setup_overlay(editor);
}
});
}
onActiveCellChanged(): void {
let active_cell = this.tracker.activeCell;
if ((active_cell !== null) && (active_cell.model.type == "markdown")) {
let editor = this.extract_editor(active_cell);
this.setup_overlay(editor);
}
}
extract_editor(cell_or_editor: any): any {
let editor_temp: any = cell_or_editor.editor;
return editor_temp._editor;
}
setup_overlay(editor: any, retry=true): void {
let current_mode: string = editor.getOption("mode");
if (current_mode == "null"){
if(retry) {
setTimeout(() => this.setup_overlay(editor, false), 0)
}
return;
}
if (this.check_spelling){
editor.setOption("mode", this.define_mode(current_mode));
}else{
let original_mode = (current_mode.match(/^spellcheck_/)) ? current_mode.substr(11) : current_mode
editor.setOption("mode", original_mode);
}
}
toggle_spellcheck(){
this.check_spelling = ! this.check_spelling;
console.log("Spell checking is currently: ", this.check_spelling);
}
setup_button(){
const command = "spellchecker:toggle-check-spelling";
this.app.commands.addCommand(command,{
label: "Check Spelling",
execute: () => {
this.toggle_spellcheck();
}
});
this.palette.addItem( {command, category: "Toggle Spell Checker"} );
}
load_dictionary(){
this.dict_promise = Promise.all([
fetch(this.aff_url).then(res=>res.text()),
fetch(this.dict_url).then(res=>res.text())
]).then((values) => {
this.dictionary = new Typo(this.lang_code, values[0], values[1]);
console.log("Dictionary Loaded ", this.lang_code);
});
}
define_mode = (original_mode_spec: string) => {
if (original_mode_spec.indexOf("spellcheck_") == 0){
return original_mode_spec;
}
var me = this;
var new_mode_spec = 'spellcheck_' + original_mode_spec;
CodeMirror.defineMode(new_mode_spec, (config:any) => {
var spellchecker_overlay = {
name: new_mode_spec,
token: function (stream:any, state:any) {
if (stream.eatWhile(me.rx_word_char)){
var word = stream.current().replace(/(^')|('$)/g, '');
if (!word.match(/^\d+$/) && (me.dictionary !== undefined) && !me.dictionary.check(word)) {
return 'spell-error';
}
}
stream.eatWhile(me.rx_non_word_char);
return null;
}
};
return CodeMirror.overlayMode(
CodeMirror.getMode(config, original_mode_spec), spellchecker_overlay, true);
});
return new_mode_spec;
}
}
/**
* Activate extension
*/
function activate(app: JupyterFrontEnd, tracker: INotebookTracker, palette: ICommandPalette, editor_tracker: IEditorTracker) {
console.log('Attempting to load spellchecker');
const sp = new SpellChecker(app, tracker, palette, editor_tracker);
console.log("Spellchecker Loaded ", sp);
};
/**
* Initialization data for the jupyterlab_spellchecker extension.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab_spellchecker',
autoStart: true,
requires: [INotebookTracker, ICommandPalette, IEditorTracker],
activate: activate
};
export default extension;