This repository was archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcodeflask.js
307 lines (227 loc) · 8.32 KB
/
codeflask.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* EditorJsCodeFlask Block for the Editor.js.
*
* @author Calum Knott ([email protected])
* @license The MIT License (MIT)
*/
/**
* @typedef {object} EditorJsCodeFlaskConfig
* @property {string} placeholder - placeholder for the empty EditorJsCodeFlask
* @property {boolean} preserveBlank - Whether or not to keep blank EditorJsCodeFlasks when saving editor data
*/
/**
* @typedef {Object} EditorJsCodeFlaskData
* @description Tool's input and output data format
* @property {String} text — EditorJsCodeFlask's content. Can include HTML tags: <a><b><i>
*/
import style from './codeflask.css'
import icon from './codeflask.svg';
import Prism from 'prismjs';
// import "prismjs-components-importer/esm"; // ALL - Massivly Increases Bundle size!
import "prismjs-components-importer/esm/prism-iecst"; // Structured Text
import "prismjs-components-importer/esm/prism-markdown";
import "prismjs-components-importer/esm/prism-json";
import "prismjs-components-importer/esm/prism-python";
import "prismjs-components-importer/esm/prism-bash";
import CodeFlask from 'codeflask';
import NiceSelect from "nice-select2/dist/js/nice-select2";
import NiceSelectStyle from "nice-select2/dist/css/nice-select2.css";
// console.log(Prism.languages)
class EditorJsCodeFlask {
/**
* Default placeholder for EditorJsCodeFlask Tool
*
* @return {string}
* @constructor
*/
static get DEFAULT_PLACEHOLDER() {
return '// Hello';
}
static get enableLineBreaks() {
return true;
}
/**
* Render plugin`s main Element and fill it with saved data
*
* @param {object} params - constructor params
* @param {EditorJsCodeFlaskData} params.data - previously saved data
* @param {EditorJsCodeFlaskConfig} params.config - user config for Tool
* @param {object} params.api - editor.js api
* @param {boolean} readOnly - read only mode flag
*/
constructor({data, config, api, readOnly}) {
// console.log(data)
this.api = api;
this.readOnly = readOnly;
this._CSS = {
block: this.api.styles.block,
wrapper: 'ce-EditorJsCodeFlask',
settingsButton: this.api.styles.settingsButton,
settingsButtonActive: this.api.styles.settingsButtonActive,
};
if (!this.readOnly) {
this.onKeyUp = this.onKeyUp.bind(this);
}
/**
* Placeholder for EditorJsCodeFlask if it is first Block
* @type {string}
*/
this._placeholder = config.placeholder ? config.placeholder : EditorJsCodeFlask.DEFAULT_PLACEHOLDER;
this._preserveBlank = config.preserveBlank !== undefined ? config.preserveBlank : false;
// this._element; // used to hold the wrapper div, as a point of reference
// let x = (x === undefined) ? your_default_value : x;
this.data = {}
this.data.code = (data.code === undefined) ? '// Hello World' : data.code;
this.data.language = (data.language === undefined) ? 'plain' : data.language;
this.data.showlinenumbers = (data.showlinenumbers === undefined) ? true : data.showlinenumbers;
this.data.editorInstance = {}
// console.log(this.data)
this._element = this.drawView();
}
/**
* Check if text content is empty and set empty string to inner html.
* We need this because some browsers (e.g. Safari) insert <br> into empty contenteditanle elements
*
* @param {KeyboardEvent} e - key up event
*/
onKeyUp(e) {
if (e.code !== 'Backspace' && e.code !== 'Delete') {
return;
}
const {textContent} = this._element;
if (textContent === '') {
this._element.innerHTML = '';
}
}
/**
* Creates a new EditorJsCodeFlask Block's view and returns it.
*
* @returns {HTMLDivElement}
*/
drawView() {
const wrapper = document.createElement('div');
wrapper.classList.add('editorjs-codeFlask_Wrapper')
let editorElem = document.createElement('div');
editorElem.classList.add('editorjs-codeFlask_Editor')
let langdisplay = document.createElement('div');
langdisplay.classList.add('editorjs-codeFlask_LangDisplay')
langdisplay.innerHTML = this.data.language
wrapper.appendChild(editorElem)
wrapper.appendChild(langdisplay)
this.data.editorInstance = new CodeFlask(editorElem, {
language: this.data.language,
lineNumbers : this.data.showlinenumbers,
readonly : this.readOnly
});
this.data.editorInstance.onUpdate((code) => {
let _length = code.split('\n').length
this._debounce(this._updateEditorHeight(_length))
});
this.data.editorInstance.addLanguage(this.data.language, Prism.languages[this.data.language]);
this.data.editorInstance.updateCode(this.data.code);
return wrapper;
}
/**
* Return Tool's view
*
* @returns {HTMLDivElement}
*/
render() {
return this._element
}
_updateEditorHeight(length){
let _height = (length * 21) + 10
if (_height < 60){ _height = 60 }
this._element.style.height = _height + 'px';
}
_debounce(func, timeout = 500){
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
renderSettings() {
const settingsContainer = document.createElement('div');
let languagesSelect = document.createElement("select");
languagesSelect.classList.add("small");
//sort available languages alphabetically (ignore case)
let languages = Object.keys(Prism.languages).sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
//Create and append the options
for (var i = 0; i < languages.length; i++) {
// Weirdly PrismJS doesnt expose a list of installed languages, or rather it does, but it is mixed with helper functions, which i have to clear here.
if (languages[i] == "extend" || languages[i] == "insertBefore" || languages[i] == "DFS") {
continue;
}
var option = document.createElement("option");
option.value = languages[i];
option.text = languages[i];
if(languages[i] == this.data.language){
option.selected="selected"
}
languagesSelect.appendChild(option);
}
languagesSelect.addEventListener('change', (event) => {
this._updateLanguage(event.target.value)
});
// Disabled until codeflask supports toggle of line numbers
// const settingsButton = document.createElement('div');
// settingsButton.classList.add(this._CSS.settingsButton);
// settingsButton.innerHTML = '<small>123</small>'
// settingsButton.addEventListener('click', (e) => {
// console.log(e)
// e.target.classList.toggle(this._CSS.settingsButtonActive)
// this._toggleLineNumbers()
// });
settingsContainer.appendChild(languagesSelect);
new NiceSelect(languagesSelect, {searchable : true, placeholder : "Language..."});
// settingsContainer.appendChild(settingsButton);
return settingsContainer;
}
_toggleLineNumbers = (thing) => {
this.data.showlinenumbers = !this.data.showlinenumbers
// replace this with a native method for codeflask, if it gets implemented.
// for now, we will completely destroy the codeflask instance, and rebuild it - lazy but effective
}
_updateLanguage = (lang) => {
this.data.language = lang
this._element.querySelector('.editorjs-codeFlask_LangDisplay').innerHTML = this.data.language
this.data.editorInstance.updateLanguage(this.data.language)
}
/**
* Extract Tool's data from the view
* @param {HTMLDivElement} toolsContent - EditorJsCodeFlask tools rendered view
* @returns {EditorJsCodeFlaskData} - saved data
* @public
*/
save(toolsContent) {
let resp = {
code : this.data.editorInstance.getCode(),
language : this.data.language,
showlinenumbers : this.data.showlinenumbers
};
return resp
}
/**
* Returns true to notify the core that read-only mode is supported
*
* @return {boolean}
*/
static get isReadOnlySupported() {
return true;
}
/**
* Icon and title for displaying at the Toolbox
*
* @return {{icon: string, title: string}}
*/
static get toolbox() {
return {
icon: icon,
title: 'CodeFlask'
};
}
}
export { EditorJsCodeFlask as default }