Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | 1x 1x 1x 15x 18x 18x 18x 19x 18x 40x 1x 1x 1x 8x 18x 18x 18x 17x 14x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 66x 66x 66x 18x 18x 324x 324x 324x 419x 18x 18x 20x 20x 20x 1x 1x 16x 66x 3x 3x 3x 3x 3x 3x 2x 2x 66x 8x 8x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 3x 3x 2x 2x 3x 3x 3x 3x 2x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 11x 11x 10x 11x 4x 4x 4x 4x 7x 7x 7x 7x 11x 13x 13x 8x 8x 8x 8x 8x 5x 5x 5x 5x 5x 5x 5x 13x | /* eslint-disable max-lines */ /* eslint max-lines: ["error", 400] */ // @ts-check import Vue from "vue"; import CodeMirror from "codemirror"; import "codemirror/addon/edit/continuelist"; import "./CodeMirrorAutoIndentAddon"; /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ // @ts-ignore import PugMod from "codemirror/mode/pug/pug.js"; // jshint ignore:line /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ // @ts-ignore import MarkdownMod from "codemirror/mode/markdown/markdown"; // jshint ignore:line import { BaseStoreMixin } from "../store/utils"; import BaseResizeObserverMixin from "../mixins/BaseResizeObserverMixin"; import { assign, bindAll, clone, cloneDeep, first, includes, isEmpty, set, split, throttle } from "lodash"; const managedModes = [ "pug", "markdown", "javascript" ]; const managedEvents = [ "scroll", "changes", "beforeChange", "cursorActivity", "keyHandled", "inputRead", "electricInput", "beforeSelectionChange", "viewportChange", "swapDoc", "gutterClick", "gutterContextMenu", "focus", "blur", "refresh", "optionChange", "scrollCursorIntoView", "update" ]; /** * @typedef {{ textarea: HTMLTextAreaElement }} Refs */ const component = /** @type {V.Constructor<any, Refs>} */ (Vue).extend({ name: "CodeMirrorEditor", mixins: [ BaseResizeObserverMixin, BaseStoreMixin ], props: { value: { type: String, default: "" }, name: { type: String, default: "codemirror" }, maxLength: { type: Number, default: -1 }, placeholder: { type: String, default: "" }, markdownTools: { type: Boolean, default: true }, options: { type: Object, default: () => ({}) }, events: { type: Array, default: () => ([]) }, managedModes: { type: Array, default: () => managedModes } }, /** * @return {{ * content: string, * codemirror: CodeMirror.EditorFromTextArea | null * cminstance: CodeMirror.EditorFromTextArea | null * cmOptions: CodeMirror.EditorConfiguration * }} */ data() { return { content: "", codemirror: null, cminstance: null, cmOptions: { mode: "markdown" } // Default mode }; }, computed: { /** * @returns {boolean} */ showMarkdownTools() { return this.cmOptions.mode === "markdown" && this.markdownTools; }, /** * @return {boolean} */ darkMode() { return this.getBaseState([ "ui", "darkMode" ]); }, /** * @returns {number} */ valueLength() { return this.cminstance?.getValue().length ?? 0; } }, watch: { options: { deep: true, /** * @param {any} options */ handler(options) { // eslint-disable-next-line guard-for-in for (const key in options) { set(this.cmOptions, key, options[key]); // @ts-ignore key is well formed Eif (this.cminstance) { this.cminstance.setOption(key, options[key]); } } } }, /** * @param {string} newVal */ value(newVal) { this.handlerCodeChange(newVal); }, darkMode: function(mode) { if (this.codemirror) { this.codemirror.setOption("theme", mode ? "material-darker" : "default"); } } }, mounted() { this.initialize(); bindAll(this, [ "bold", "italic", "list", "link", "undo", "redo" ]); this.onResize = throttle(this.onResize, 250 /* ms */); }, beforeDestroy() { Eif (this.cminstance) { this.cminstance.toTextArea(); } }, methods: { /** * @param {number} width * @param {number} height */ onResize(width, height) { // start auto-refresh if (width > 0 && height > 0) { // @ts-ignore const display = this.cminstance?.display; // eslint-disable-next-line eqeqeq Eif (display.lastWrapHeight == display.wrapper.clientHeight) { return; } this.refresh(); } }, /** * @brief CodeMirror Management */ initialize() { // Only markdown or pug mode at the moment. Default is markdown /** @type CodeMirror.EditorConfiguration */ const cmOptions = assign(this.cmOptions, this.options); // If markdown mode, add extrakeys and addons Eif (cmOptions.mode === "markdown") { assign(cmOptions, { extraKeys: { "Ctrl-B": this.bold, "Cmd-B": this.bold, "Ctrl-I": this.italic, "Cmd-I": this.italic, "Ctrl-L": this.list, "Cmd-L": this.list, "Ctrl-K": this.link, "Cmd-K": this.link, "Ctrl-Z": this.undo, "Cmd-Z": this.undo, "Shift-Ctrl-Z": this.redo, "Shift-Cmd-Z": this.redo, Enter: "newlineAndIndentContinueMarkdownList", Tab: "autoIndentMarkdownList", "Shift-Tab": "autoUnindentMarkdownList" } }); } Iif (!includes(this.managedModes, cmOptions.mode)) { this.$refs.textarea.innerHTML = `Mode ${cmOptions.mode} not supported...`; return; } cmOptions.theme = this.darkMode ? "material-darker" : "default"; this.codemirror = CodeMirror.fromTextArea(this.$refs.textarea, cmOptions); this.cminstance = this.codemirror; this.cmOptions = cmOptions; this.cminstance.setValue(this.value || this.content); this.cminstance.on("beforeChange", this.enforceMaxLength); this.cminstance.on("change", (/** @type CodeMirror.Editor */cm) => { this.content = cm.getValue(); Eif (this.$emit) { this.$emit("input", this.content); } }); /** @type any */ const tmpEvents = {}; clone(managedEvents) // @ts-ignore .concat(this.events) .filter((/** @type string */ e) => (!tmpEvents[e] && (tmpEvents[e] = true))) .forEach((event) => { Iif (!this.cminstance) { return; } // @ts-ignore event is inside managedEvents this.cminstance.on(event, (/** @type any[] */...args) => { this.$emit(event, ...args); }); }); this.$emit("ready", this.codemirror); // prevents funky dynamic rendering this.refresh(); }, refresh() { this.$nextTick(() => { Iif (!this.cminstance) { return; } this.cminstance.refresh(); }); }, focus() { Iif (!this.cminstance) { return; } this.cminstance.focus(); }, /** * @returns { CodeMirror.Editor | null } */ get() { return this.cminstance; }, /** * @param {CodeMirror.Editor} cm * @param {any} change */ enforceMaxLength(cm, change) { if (this.maxLength > 0 && change.update) { const separator = cm.getDoc().lineSeparator(); var str = change.text.join(separator); var delta = str.length - (cm.indexFromPos(change.to) - cm.indexFromPos(change.from)); Iif (delta <= 0) { return true; } delta = cm.getValue().length + delta - this.maxLength; if (delta > 0) { str = str.substr(0, str.length - delta); change.update(change.from, change.to, str.split(separator)); } } return undefined; }, /** * @param {string} newVal */ handlerCodeChange(newVal) { Iif (!this.cminstance) { return; } const cmValue = this.cminstance.getValue(); if (newVal !== cmValue) { /** @type CodeMirror.ScrollInfo */ const scrollInfo = this.cminstance.getScrollInfo(); this.cminstance.setValue(newVal); this.content = newVal; this.cminstance.scrollTo(scrollInfo.left, scrollInfo.top); } }, getForwardSelection() { Iif (!this.cminstance) { return null; } const range = cloneDeep(first(this.cminstance.listSelections())); Iif (!range) { return null; } Iif (range.anchor.line > range.head.line || (range.anchor.line === range.head.line && range.anchor.ch > range.head.ch)) { const tmp = range.head; range.head = range.anchor; range.anchor = tmp; } return range; }, /** * @brief Toolbar for Markdown mode */ heading() { Iif (!this.cminstance) { return; } const withSpace = this.cminstance.getSelection() === this.cminstance.getLine(this.cminstance.getCursor().line); this.makeListWith("#", withSpace); }, bold() { this.wrapSelectionWith("**"); }, italic() { this.wrapSelectionWith("_"); }, strikethrough() { this.wrapSelectionWith("~~"); }, quote() { this.makeListWith(">"); }, code() { Iif (!this.cminstance) { return; } const selected = this.cminstance.getSelection(); const nLines = split(selected, "\n").length; if (nLines <= 1) { this.wrapSelectionWith("`"); } else { const range = this.getForwardSelection(); Iif (!range) { return; } const elStart = range.anchor.ch === 0 ? "```\n" : "\n```\n"; const lastLine = clone(this.cminstance.getLine(range.head.line)); const elEnd = range.head.ch === lastLine.length ? "\n```" : "\n```\n"; this.wrapSelectionWith(elStart, elEnd); } }, link() { Iif (!this.cminstance) { return; } const initialCursor = clone(this.cminstance.getCursor("to")); const selected = this.cminstance.getSelection(); // It may be empty // Advance and set the cursor at the beginning of "url" text const startCursor = { ch: initialCursor.ch + 3 /* for []( */, line: initialCursor.line }; const endCursor = { ch: startCursor.ch + 3, // "url" line: startCursor.line }; this.cminstance.replaceSelection(`[${selected}](url)`); this.cminstance.focus(); this.cminstance.setSelection(startCursor, endCursor); }, list() { this.makeListWith("-"); }, taskList() { this.makeListWith("- [ ]"); }, table() { Iif (!this.cminstance) { return; } const tableStr = "\n| header | header |\n" + "| ------ | ------ |\n" + "| cell | cell |\n" + "| cell | cell |\n"; const cursor = this.cminstance.getCursor(); this.cminstance.replaceRange(tableStr, cursor, cursor); }, numberedList() { Iif (!this.cminstance) { return; } if (!this.cminstance.somethingSelected()) { this.makeListWith("1."); return; } const selected = this.cminstance.getSelection(); const lines = split(selected, "\n"); let listed = ""; let number = 1; for (const line of lines) { Iif (isEmpty(line)) { continue; } listed += `${number++}. ${line}\n`; } this.cminstance.replaceSelection(`${listed}`); }, indent() { Iif (!this.cminstance) { return; } this.indentLines("add"); this.cminstance.focus(); }, outdent() { Iif (!this.cminstance) { return; } this.indentLines("subtract"); this.cminstance.focus(); }, /** * @param {"add" | "subtract" } mode */ indentLines(mode) { Iif (!this.cminstance) { return; } Iif (mode !== "add" && mode !== "subtract") { return; } const range = this.getForwardSelection(); Iif (!range) { return; } for (let line = range.anchor.line; line <= range.head.line; line++) { this.cminstance.indentLine(line, mode); } }, undo() { Iif (!this.cminstance) { return; } this.cminstance.undo(); }, redo() { Iif (!this.cminstance) { return; } this.cminstance.redo(); }, /** * @param {string} elementStart * @param {string?=} elementEnd */ wrapSelectionWith(elementStart, elementEnd = undefined) { Iif (!this.cminstance) { return; } if (!elementEnd) { elementEnd = elementStart; } if (!this.cminstance.somethingSelected()) { // Add element start and end and move cursor in the middle const initialCursor = this.cminstance.getCursor(); this.cminstance.replaceSelection(`${elementStart}${elementEnd}`); // Advance and set the cursor initialCursor.ch += elementStart.length; this.cminstance.setCursor(initialCursor); } else { const selection = first(this.cminstance.listSelections()); const selected = this.cminstance.getSelection(); Iif (!selection) { return; } this.cminstance.replaceRange(`${elementStart}${selected}${elementEnd}`, selection.anchor, selection.head, selected); } this.cminstance.focus(); }, /** * @param {string} elementList * @param {boolean} withSpace */ makeListWith(elementList, withSpace = true) { Iif (!this.cminstance) { return; } if (!this.cminstance.somethingSelected()) { // Add element list and move cursor at the end const initialCursor = this.cminstance.getCursor(); const line = this.cminstance.getLine(initialCursor.line); this.cminstance.replaceRange(`${elementList}${withSpace ? " " : ""}${line}`, { line: initialCursor.line, ch: 0 }, // @ts-ignore: ch is set to last position automatically { line: initialCursor.line }, line); // This replace the whole line // Advance and set the cursor initialCursor.ch += elementList.length + 1; this.cminstance.setCursor(initialCursor); } else { const selected = this.cminstance.getSelection(); const lines = split(selected, "\n"); let listed = ""; for (const line of lines) { Iif (isEmpty(line)) { continue; } listed += `${elementList}${withSpace ? " " : ""}${line}\n`; } this.cminstance.replaceSelection(`${listed}`); } this.cminstance.focus(); } } }); export default component; |