-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(monaco-editor): 实现 monaco 编辑器初始化 | Implement monaco editor initi…
…alization.
- Loading branch information
1 parent
045b78b
commit 7d5ac79
Showing
15 changed files
with
371 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!-- | ||
Copyright (C) 2023 Zuoqiu Yingyi | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as | ||
published by the Free Software Foundation, either version 3 of the | ||
License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Editor</title> | ||
<script async type="module" src="~/src/main.ts"></script> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!-- | ||
Copyright (C) 2023 Zuoqiu Yingyi | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as | ||
published by the Free Software Foundation, either version 3 of the | ||
License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<script async type="module" src="./src/main.ts"></script> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"displayName": "Code Editor" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"displayName": "代碼編輯器" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"displayName": "代码编辑器" | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<!-- | ||
Copyright (C) 2023 Zuoqiu Yingyi | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as | ||
published by the Free Software Foundation, either version 3 of the | ||
License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<script lang="ts"> | ||
import { onMount } from "svelte"; | ||
import type { editor } from "monaco-editor"; | ||
import loader from "@monaco-editor/loader"; | ||
import { mapLocale } from "@/utils/locale"; | ||
import { merge } from "@workspace/utils/misc/merge"; | ||
export let locale = "zh-Hans"; // 界面语言 | ||
export let content = ""; // 编辑器内容 | ||
export let language = "plaintext"; // 编辑器语言模式 | ||
export let options: editor.IStandaloneEditorConstructionOptions = {}; // 编辑器配置 | ||
let editorElement; | ||
loader.config({ | ||
paths: { | ||
vs: (() => { | ||
switch (true) { | ||
case import.meta.env.DEV: | ||
return "node_modules/monaco-editor/min/vs"; | ||
case import.meta.env.PROD: | ||
default: | ||
return "./../libs/monaco-editor/min/vs"; | ||
} | ||
})(), | ||
}, | ||
// monaco, | ||
"vs/nls": { | ||
availableLanguages: { | ||
"*": mapLocale(locale), | ||
}, | ||
}, | ||
}); | ||
const init = loader.init(); | ||
onMount(() => { | ||
init.then(monaco => { | ||
const editor = monaco.editor.create( | ||
editorElement, | ||
merge(options, { | ||
value: content, | ||
language: language, | ||
}), | ||
); | ||
}); | ||
}); | ||
</script> | ||
|
||
<!-- REF: https://www.svelte.cn/docs#bind_element --> | ||
<div | ||
bind:this={editorElement} | ||
class="editor" | ||
/> | ||
|
||
<style lang="less"> | ||
.editor { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!-- | ||
Copyright (C) 2023 Zuoqiu Yingyi | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as | ||
published by the Free Software Foundation, either version 3 of the | ||
License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<script lang="ts"> | ||
import Editor from "./Editor.svelte"; | ||
</script> | ||
|
||
<Editor /> | ||
|
||
<style lang="less"> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (C) 2023 Zuoqiu Yingyi | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* 插件入口 */ | ||
import siyuan from "siyuan"; | ||
|
||
export default class MonacoEditorPlugin extends siyuan.Plugin { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (C) 2023 Zuoqiu Yingyi | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* 界面入口 */ | ||
import Main from "./components/Main.svelte"; | ||
import "./styles/main.less"; | ||
|
||
/** | ||
* Monaco Editor 环境 | ||
* REF: https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md#using-vite | ||
*/ | ||
self.MonacoEnvironment = { | ||
getWorker: function (workerId, label) { | ||
const getWorkerModule = (moduleUrl, label) => { | ||
return new Worker(self.MonacoEnvironment.getWorkerUrl(moduleUrl, label), { | ||
name: label, | ||
type: "module", | ||
}); | ||
}; | ||
|
||
switch (label) { | ||
case "json": | ||
return getWorkerModule("/monaco-editor/esm/vs/language/json/json.worker?worker", label); | ||
case "css": | ||
case "scss": | ||
case "less": | ||
return getWorkerModule("/monaco-editor/esm/vs/language/css/css.worker?worker", label); | ||
case "html": | ||
case "handlebars": | ||
case "razor": | ||
return getWorkerModule("/monaco-editor/esm/vs/language/html/html.worker?worker", label); | ||
case "js": | ||
case "ts": | ||
case "javascript": | ||
case "typescript": | ||
return getWorkerModule("/monaco-editor/esm/vs/language/typescript/ts.worker?worker", label); | ||
default: | ||
return getWorkerModule("/monaco-editor/esm/vs/editor/editor.worker?worker", label); | ||
} | ||
}, | ||
}; | ||
|
||
const main = new Main({ | ||
target: globalThis.document.body, | ||
props: { | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
html, | ||
body { | ||
width: 100%; | ||
height: 100%; | ||
|
||
margin: 0; | ||
border: 0; | ||
padding: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (C) 2023 Zuoqiu Yingyi | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { | ||
i18nCheck, | ||
i18nChecks, | ||
} from "@workspace/types/siyuan/i18n"; | ||
|
||
import zh_Hans from "~/public/i18n/zh_CN.json"; | ||
import zh_Hant from "~/public/i18n/zh_CHT.json"; | ||
import en from "~/public/i18n/en_US.json"; | ||
|
||
export type I18N = typeof zh_Hans; | ||
|
||
i18nChecks([ | ||
i18nCheck<I18N, typeof zh_Hans>(), | ||
i18nCheck<I18N, typeof zh_Hant>(), | ||
i18nCheck<I18N, typeof en>(), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (C) 2023 Zuoqiu Yingyi | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { mapLang } from "@workspace/utils/locale/language"; | ||
|
||
// REF: https://www.npmjs.com/package/monaco-editor-nls | ||
export function mapLocale(locale: string): string { | ||
switch (mapLang(locale)) { | ||
case "zh-Hans": | ||
return "zh-cn"; | ||
case "zh-Hant": | ||
return "zh-tw"; | ||
|
||
case "de": | ||
return "de"; | ||
case "es": | ||
return "es"; | ||
case "fr": | ||
return "fr"; | ||
case "it": | ||
return "it"; | ||
case "ja": | ||
return "ja"; | ||
case "ko": | ||
return "ko"; | ||
case "ru": | ||
return "ru"; | ||
|
||
case "en": | ||
default: | ||
return "en"; | ||
} | ||
} |
Oops, something went wrong.