|
1 | | -import prettier from "prettier/standalone"; |
2 | | -import pretterParserHTML from "prettier/parser-html"; |
3 | | -import prettierParserBabel from "prettier/parser-babel"; |
4 | | -import prettierParserGraphql from "prettier/parser-graphql"; |
5 | | -import prettierParserAngular from "prettier/parser-angular"; |
6 | | -import prettierParserAspree from "prettier/parser-espree"; |
7 | | -import prettierParserFlow from "prettier/parser-flow"; |
8 | | -import prettierParserGlimmer from "prettier/parser-glimmer"; |
9 | | -import prettierParserMd from "prettier/parser-markdown"; |
10 | | -import prettierParserMeriyah from "prettier/parser-meriyah"; |
11 | | -import prettierParserPostcss from "prettier/parser-postcss"; |
12 | | -import prettierParserTypescript from "prettier/parser-typescript"; |
13 | | -import prettierParserYaml from "prettier/parser-yaml"; |
14 | | -import plugin from '../plugin.json'; |
15 | | - |
16 | | -const plugins = [ |
17 | | - prettierParserBabel, |
18 | | - prettierParserGraphql, |
19 | | - prettierParserAngular, |
20 | | - prettierParserAspree, |
21 | | - prettierParserFlow, |
22 | | - prettierParserGlimmer, |
23 | | - pretterParserHTML, |
24 | | - prettierParserMd, |
25 | | - prettierParserMeriyah, |
26 | | - prettierParserPostcss, |
27 | | - prettierParserTypescript, |
28 | | - prettierParserYaml, |
29 | | -]; |
| 1 | +import plugin from "../plugin.json"; |
30 | 2 |
|
31 | 3 | const pluginId = plugin.id; |
32 | 4 |
|
33 | | -class Prettier { |
| 5 | +class AcodePrettier { |
| 6 | + worker = null; |
| 7 | + workerInitialized = false; |
| 8 | + |
34 | 9 | constructor() { |
35 | 10 | this.run = this.run.bind(this); |
36 | 11 | } |
| 12 | + |
| 13 | + async loadScript() { |
| 14 | + if (document.getElementById("prettier-vendor-script")) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const $script = document.createElement("script"); |
| 19 | + $script.id = "prettier-vendor-script"; |
| 20 | + $script.src = acode.joinUrl(this.baseUrl, "vendor.js"); |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + $script.onload = resolve; |
| 23 | + $script.onerror = reject; |
| 24 | + }); |
| 25 | + } |
| 26 | + |
37 | 27 | static inferParser(filename) { |
38 | 28 | switch (filename.slice(filename.lastIndexOf(".") + 1)) { |
39 | 29 | case "html": |
@@ -83,6 +73,12 @@ class Prettier { |
83 | 73 | } |
84 | 74 |
|
85 | 75 | async init() { |
| 76 | + if (typeof Worker !== undefined) { |
| 77 | + this.#initializeWorker(); |
| 78 | + } else { |
| 79 | + await this.loadScript(); |
| 80 | + } |
| 81 | + |
86 | 82 | const config = appSettings.value[pluginId]; |
87 | 83 | const extensions = [ |
88 | 84 | "html", |
@@ -113,16 +109,33 @@ class Prettier { |
113 | 109 | const { editor, activeFile } = editorManager; |
114 | 110 | const code = editor.getValue(); |
115 | 111 | const cursorPos = editor.getCursorPosition(); |
116 | | - const parser = Prettier.inferParser(activeFile.name); |
117 | | - const res = prettier.formatWithCursor(code, { |
118 | | - parser, |
119 | | - cursorOffset: this.#cursorPosTocursorOffset(cursorPos), |
120 | | - filepath: activeFile.name, |
121 | | - plugins, |
| 112 | + const parser = AcodePrettier.inferParser(activeFile.name); |
| 113 | + |
| 114 | + if (typeof Worker === undefined) { |
| 115 | + const { prettier, plugins } = window.acodePluginPrettier; |
| 116 | + const res = prettier.formatWithCursor(code, { |
| 117 | + parser, |
| 118 | + cursorOffset: this.#cursorPosTocursorOffset(cursorPos), |
| 119 | + filepath: activeFile.name, |
| 120 | + plugins, |
| 121 | + }); |
| 122 | + editor.setValue(res.formatted); |
| 123 | + const { row, column } = this.#cursorOffsetTocursorPos( |
| 124 | + res.cursorOffset |
| 125 | + ); |
| 126 | + editor.gotoLine(row + 1, column - 1); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + this.worker.postMessage({ |
| 131 | + id: activeFile.id, |
| 132 | + code, |
| 133 | + cursorOptions: { |
| 134 | + parser, |
| 135 | + cursorOffset: this.#cursorPosTocursorOffset(cursorPos), |
| 136 | + filepath: activeFile.name, |
| 137 | + }, |
122 | 138 | }); |
123 | | - editor.setValue(res.formatted); |
124 | | - const { row, column } = this.#cursorOffsetTocursorPos(res.cursorOffset); |
125 | | - editor.gotoLine(row + 1, column - 1); |
126 | 139 | } |
127 | 140 |
|
128 | 141 | destroy() { |
@@ -157,17 +170,49 @@ class Prettier { |
157 | 170 | column, |
158 | 171 | }; |
159 | 172 | } |
| 173 | + |
| 174 | + #initializeWorker() { |
| 175 | + this.worker = new Worker(new URL("./worker.js", import.meta.url)); |
| 176 | + this.worker.onmessage = (e) => { |
| 177 | + const { id, res, action } = e.data; |
| 178 | + if (action === "script loaded") { |
| 179 | + this.workerInitialized = true; |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + if (action === "code format") { |
| 184 | + const file = editorManager.getFile(id, "id"); |
| 185 | + if (!file) return; |
| 186 | + |
| 187 | + const { session } = file; |
| 188 | + session.setValue(res.formatted); |
| 189 | + const { row, column } = this.#cursorOffsetTocursorPos( |
| 190 | + res.cursorOffset |
| 191 | + ); |
| 192 | + |
| 193 | + session.selection.moveCursorTo(row, column); |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + this.worker.postMessage({ |
| 198 | + action: "load script", |
| 199 | + scriptUrl: acode.joinUrl(this.baseUrl, "vendor.js"), |
| 200 | + }); |
| 201 | + } |
160 | 202 | } |
161 | 203 |
|
162 | 204 | if (window.acode) { |
163 | | - const prettier = new Prettier(); |
164 | | - acode.setPluginInit(pluginId, (baseUrl, $page, { cacheFileUrl, cacheFile }) => { |
165 | | - if (!baseUrl.endsWith("/")) { |
166 | | - baseUrl += "/"; |
| 205 | + const prettier = new AcodePrettier(); |
| 206 | + acode.setPluginInit( |
| 207 | + pluginId, |
| 208 | + (baseUrl, $page, { cacheFileUrl, cacheFile }) => { |
| 209 | + if (!baseUrl.endsWith("/")) { |
| 210 | + baseUrl += "/"; |
| 211 | + } |
| 212 | + prettier.baseUrl = baseUrl; |
| 213 | + prettier.init($page, cacheFile, cacheFileUrl); |
167 | 214 | } |
168 | | - prettier.baseUrl = baseUrl; |
169 | | - prettier.init($page, cacheFile, cacheFileUrl); |
170 | | - }); |
| 215 | + ); |
171 | 216 | acode.setPluginUnmount(pluginId, () => { |
172 | 217 | prettier.destroy(); |
173 | 218 | }); |
|
0 commit comments