nuxt-monaco-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ## 1.0.0 (2022-08-25)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 e_chan1007
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # nuxt-monaco-editor
2
+ [![npm version](https://badge.fury.io/js/nuxt-monaco-editor.svg)](https://badge.fury.io/js/nuxt-monaco-editor)
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/8b4585be9901491795f8b3c2f5dbb680)](https://www.codacy.com/gh/e-chan1007/nuxt-monaco-editor/dashboard?utm_source=github.com&utm_medium=referral&utm_content=e-chan1007/nuxt-monaco-editor&utm_campaign=Badge_Grade)
5
+
6
+ Integrate [monaco-editor](https://microsoft.github.io/monaco-editor/) with Nuxt
7
+
8
+ ## Install
9
+ ```sh
10
+ # npm
11
+ npm install -D monaco-editor nuxt-monaco-editor
12
+
13
+ # yarn
14
+ yarn add -D monaco-editor nuxt-monaco-editor
15
+
16
+ # pnpm
17
+ pnpm add -D monaco-editor nuxt-monaco-editor
18
+ ```
19
+ Don't forget to install `monaco-editor`.
20
+
21
+ ## Setup
22
+ 1. Add this module to the Nuxt config
23
+
24
+ ```ts
25
+ export default defineNuxtConfig({
26
+ modules: [
27
+ 'nuxt-monaco-editor'
28
+ ]
29
+ })
30
+ ```
31
+
32
+ 2. (Optional) Configure the module
33
+
34
+ ```ts
35
+ export default defineNuxtConfig({
36
+ monacoEditor: {
37
+ // These are default values:
38
+ locale: 'en',
39
+ componentName: {
40
+ codeEditor: 'MonacoEditor',
41
+ diffEditor: 'MonacoDiffEditor'
42
+ }
43
+ }
44
+ })
45
+ ```
46
+
47
+ 3. Use the component in your pages or components
48
+
49
+ ```vue
50
+ <template>
51
+ <MonacoEditor v-model="value" lang="typescript" />
52
+ </template>
53
+
54
+ <script lang="ts" setup>
55
+ const value = ref('')
56
+ </script>
57
+ ```
58
+
59
+ ## Development
60
+
61
+ - Run `npm run dev:prepare` to generate type stubs.
62
+ - Use `npm run dev` to start [playground](./playground) in development mode.
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,13 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ declare type MonacoEditorLocale = 'de' | 'es' | 'fr' | 'it' | 'ja' | 'ko' | 'ru' | 'zh-cn' | 'zh-tw' | 'en';
4
+ interface ModuleOptions {
5
+ locale?: MonacoEditorLocale;
6
+ componentName?: {
7
+ codeEditor?: string;
8
+ diffEditor?: string;
9
+ };
10
+ }
11
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
12
+
13
+ export { ModuleOptions, MonacoEditorLocale, _default as default };
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "nuxt-monaco-editor",
3
+ "configKey": "monacoEditor",
4
+ "compatibility": {
5
+ "nuxt": "^3.0.0"
6
+ },
7
+ "version": "1.0.0"
8
+ }
@@ -0,0 +1,49 @@
1
+ import { fileURLToPath } from 'url';
2
+ import { defineNuxtModule, createResolver, addVitePlugin, addPlugin, addComponent, addAutoImport } from '@nuxt/kit';
3
+ import { viteStaticCopy } from 'vite-plugin-static-copy';
4
+
5
+ // -- Unbuild CommonJS Shims --
6
+ import __cjs_url__ from 'url';
7
+ import __cjs_path__ from 'path';
8
+ import __cjs_mod__ from 'module';
9
+ const __filename = __cjs_url__.fileURLToPath(import.meta.url);
10
+ const __dirname = __cjs_path__.dirname(__filename);
11
+ const require = __cjs_mod__.createRequire(import.meta.url);
12
+
13
+
14
+ const DEFAULTS = {
15
+ locale: "en",
16
+ componentName: {
17
+ codeEditor: "MonacoEditor",
18
+ diffEditor: "MonacoDiffEditor"
19
+ }
20
+ };
21
+ const module = defineNuxtModule({
22
+ meta: {
23
+ name: "nuxt-monaco-editor",
24
+ configKey: "monacoEditor",
25
+ compatibility: { nuxt: "^3.0.0" }
26
+ },
27
+ defaults: DEFAULTS,
28
+ setup(options, nuxt) {
29
+ const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
30
+ const { resolve } = createResolver(runtimeDir);
31
+ nuxt.options.build.transpile.push(runtimeDir);
32
+ nuxt.options.build.transpile.push("monaco-editor");
33
+ nuxt.options.runtimeConfig.app.__MONACO_EDITOR_LOCALE__ = options.locale;
34
+ const [viteStaticCopyServePlugin, viteStaticCopyBuildPlugin] = viteStaticCopy({
35
+ targets: [{
36
+ src: require.resolve("monaco-editor/min/vs/loader.js").replace(/\/vs\/loader\.js$/, "/*"),
37
+ dest: "_monaco"
38
+ }]
39
+ });
40
+ addVitePlugin(viteStaticCopyServePlugin);
41
+ addVitePlugin(viteStaticCopyBuildPlugin);
42
+ addPlugin(resolve("plugin.client"));
43
+ addComponent({ name: options.componentName.codeEditor, filePath: resolve("MonacoEditor.vue") });
44
+ addComponent({ name: options.componentName.diffEditor, filePath: resolve("MonacoDiffEditor.vue") });
45
+ addAutoImport({ name: "useMonaco", as: "useMonaco", from: resolve("composables") });
46
+ }
47
+ });
48
+
49
+ export { module as default };
@@ -0,0 +1,95 @@
1
+ <template>
2
+ <div ref="editorElement">
3
+ <slot v-if="isLoading" />
4
+ </div>
5
+ </template>
6
+
7
+ <script lang="ts" setup>
8
+ import type * as Monaco from 'monaco-editor'
9
+ import { onMounted, ref, watch } from 'vue'
10
+ import { useMonaco } from './composables'
11
+
12
+ interface Props {
13
+ /**
14
+ * Programming Language (Not a locale for UI);
15
+ * overrides `options.language`
16
+ */
17
+ lang?: string;
18
+ /**
19
+ * Options passed to the second argument of `monaco.editor.createDiffEditor`
20
+ */
21
+ options?: Monaco.editor.IStandaloneDiffEditorConstructionOptions
22
+ original?: string;
23
+ modelValue?: string;
24
+ }
25
+
26
+ interface Emits {
27
+ (event: 'update:modelValue', value: string): void
28
+ }
29
+
30
+ const props = withDefaults(defineProps<Props>(), {
31
+ lang: () => 'plaintext',
32
+ options: () => ({}),
33
+ original: () => '',
34
+ modelValue: () => ''
35
+ })
36
+ const emit = defineEmits<Emits>()
37
+ const isLoading = ref(true)
38
+
39
+ const editorElement = ref<HTMLDivElement>()
40
+ const monaco = useMonaco()!
41
+
42
+ let ignoreWatch = false
43
+ let editor: Monaco.editor.IStandaloneDiffEditor
44
+ let originalModel: Monaco.editor.ITextModel
45
+ let modifiedModel: Monaco.editor.ITextModel
46
+
47
+ watch(() => [props.original, props.modelValue], () => {
48
+ if (ignoreWatch) {
49
+ ignoreWatch = false
50
+ return
51
+ }
52
+ originalModel.setValue(props.original)
53
+ modifiedModel.setValue(props.modelValue)
54
+ })
55
+
56
+ watch(() => props.lang, () => {
57
+ if (originalModel) { originalModel.dispose() }
58
+ if (modifiedModel) { originalModel.dispose() }
59
+ originalModel = monaco.editor.createModel(props.original, props.lang)
60
+ modifiedModel = monaco.editor.createModel(props.modelValue, props.lang)
61
+ editor.setModel({
62
+ original: originalModel,
63
+ modified: modifiedModel
64
+ })
65
+ })
66
+
67
+ watch(() => props.options, () => {
68
+ editor?.updateOptions(props.options)
69
+ })
70
+
71
+ defineExpose({
72
+ /**
73
+ * Monaco editor instance
74
+ */
75
+ // @ts-ignore
76
+ $editor: editor
77
+ })
78
+
79
+ onMounted(() => {
80
+ editor = monaco.editor.createDiffEditor(editorElement.value!, props.options)
81
+ originalModel = monaco.editor.createModel(props.original, props.lang)
82
+ modifiedModel = monaco.editor.createModel(props.modelValue, props.lang)
83
+ editor.setModel({
84
+ original: originalModel,
85
+ modified: modifiedModel
86
+ })
87
+
88
+ editor.onDidUpdateDiff(() => {
89
+ ignoreWatch = true
90
+ emit('update:modelValue', editor.getModel()!.modified.getValue())
91
+ })
92
+
93
+ isLoading.value = false
94
+ })
95
+ </script>
@@ -0,0 +1,85 @@
1
+ <template>
2
+ <div ref="editorElement">
3
+ <slot v-if="isLoading" />
4
+ </div>
5
+ </template>
6
+
7
+ <script lang="ts" setup>
8
+ import { computed } from '@vue/reactivity'
9
+ import type * as Monaco from 'monaco-editor'
10
+ import { onMounted, ref, watch } from 'vue'
11
+ import { useMonaco } from './composables'
12
+
13
+ interface Props {
14
+ /**
15
+ * Programming Language (Not a locale for UI);
16
+ * overrides `options.language`
17
+ */
18
+ lang?: string;
19
+ /**
20
+ * Options passed to the second argument of `monaco.editor.create`
21
+ */
22
+ options?: Monaco.editor.IStandaloneEditorConstructionOptions
23
+ modelValue?: string;
24
+ }
25
+
26
+ interface Emits {
27
+ (event: 'update:modelValue', value: string): void
28
+ }
29
+
30
+ const props = withDefaults(defineProps<Props>(), {
31
+ lang: () => 'plaintext',
32
+ options: () => ({}),
33
+ modelValue: () => ''
34
+ })
35
+ const emit = defineEmits<Emits>()
36
+ const isLoading = ref(true)
37
+
38
+ const lang = computed(() => props.lang || props.options.language)
39
+
40
+ const editorElement = ref<HTMLDivElement>()
41
+ const monaco = useMonaco()!
42
+
43
+ let ignoreWatch = false
44
+ let editor: Monaco.editor.IStandaloneCodeEditor
45
+ let model: Monaco.editor.ITextModel
46
+
47
+ watch(() => props.modelValue, () => {
48
+ if (ignoreWatch) {
49
+ ignoreWatch = false
50
+ return
51
+ }
52
+ editor?.setValue(props.modelValue)
53
+ })
54
+
55
+ watch(() => props.lang, () => {
56
+ if (model) { model.dispose() }
57
+ model = monaco.editor.createModel(props.modelValue, lang.value)
58
+ editor?.setModel(model)
59
+ })
60
+
61
+ watch(() => props.options, () => {
62
+ editor?.updateOptions(props.options)
63
+ })
64
+
65
+ defineExpose({
66
+ /**
67
+ * Monaco editor instance
68
+ */
69
+ // @ts-ignore
70
+ $editor: editor
71
+ })
72
+
73
+ onMounted(() => {
74
+ editor = monaco.editor.create(editorElement.value!, props.options)
75
+ model = monaco.editor.createModel(props.modelValue, lang.value)
76
+ editor.setModel(model)
77
+
78
+ editor.onDidChangeModelContent(() => {
79
+ ignoreWatch = true
80
+ emit('update:modelValue', editor.getValue())
81
+ })
82
+
83
+ isLoading.value = false
84
+ })
85
+ </script>
@@ -0,0 +1,7 @@
1
+ import type * as Monaco from 'monaco-editor';
2
+ export declare const _useMonacoState: () => any;
3
+ /**
4
+ * Get `monaco` namespace
5
+ * @returns `monaco` namespace: if unavailable (server-side), returns `null`
6
+ */
7
+ export declare const useMonaco: () => typeof Monaco | null;
@@ -0,0 +1,3 @@
1
+ import { useState } from "#app";
2
+ export const _useMonacoState = () => useState("MonacoEditorNamespace", () => null);
3
+ export const useMonaco = () => _useMonacoState().value;
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,25 @@
1
+ import { defineNuxtPlugin, useRuntimeConfig } from "#app";
2
+ import { _useMonacoState } from "./composables.mjs";
3
+ export default defineNuxtPlugin((_nuxtApp) => new Promise((resolve) => {
4
+ const monacoState = _useMonacoState();
5
+ const locale = useRuntimeConfig().app.__MONACO_EDITOR_LOCALE__;
6
+ const script = document.createElement("script");
7
+ script.src = "/_monaco/vs/loader.js";
8
+ script.onload = () => {
9
+ require.config({ paths: { vs: "_monaco/vs" } });
10
+ if (locale !== "en") {
11
+ require.config({
12
+ "vs/nls": {
13
+ availableLanguages: {
14
+ "*": locale
15
+ }
16
+ }
17
+ });
18
+ }
19
+ require(["vs/editor/editor.main"], (monaco) => {
20
+ monacoState.value = monaco;
21
+ resolve();
22
+ });
23
+ };
24
+ document.head.appendChild(script);
25
+ }));
@@ -0,0 +1,10 @@
1
+
2
+ import { ModuleOptions } from './module'
3
+
4
+ declare module '@nuxt/schema' {
5
+ interface NuxtConfig { ['monacoEditor']?: Partial<ModuleOptions> }
6
+ interface NuxtOptions { ['monacoEditor']?: ModuleOptions }
7
+ }
8
+
9
+
10
+ export { default } from './module'
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "nuxt-monaco-editor",
3
+ "author": "e-chan1007",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/e-chan1007/nuxt-monaco-editor.git"
7
+ },
8
+ "version": "1.0.0",
9
+ "type": "module",
10
+ "license": "MIT",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/module.mjs",
14
+ "require": "./dist/module.cjs"
15
+ }
16
+ },
17
+ "main": "./dist/module.cjs",
18
+ "types": "./dist/types.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "postpack": "pinst --enable",
24
+ "_postinstall": "husky install",
25
+ "build": "nuxt-module-build",
26
+ "lint": "eslint --ext .js,.cjs,.mjs,.ts,.cts,.mts,.vue . && remark . -q",
27
+ "prepack": "pinst --disable && nuxt-module-build",
28
+ "release": "standard-version",
29
+ "dev": "nuxi dev playground",
30
+ "dev:build": "nuxi build playground",
31
+ "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
32
+ "docs:dev": "vuepress dev docs --port 3000",
33
+ "docs:build": "vuepress build docs",
34
+ "docs:serve": "vuepress serve docs --port 3000"
35
+ },
36
+ "dependencies": {
37
+ "@nuxt/kit": "^3.0.0-rc.8",
38
+ "vite-plugin-static-copy": "^0.7.0"
39
+ },
40
+ "devDependencies": {
41
+ "@commitlint/cli": "^17.0.3",
42
+ "@commitlint/config-conventional": "^17.0.3",
43
+ "@nuxt/module-builder": "latest",
44
+ "@nuxtjs/eslint-config-typescript": "latest",
45
+ "eslint": "latest",
46
+ "husky": "^8.0.1",
47
+ "monaco-editor": "^0.34.0",
48
+ "nuxt": "^3.0.0-rc.8",
49
+ "pinst": "^3.0.0",
50
+ "remark-cli": "^11.0.0",
51
+ "remark-lint": "^9.1.1",
52
+ "remark-preset-lint-recommended": "^6.1.2",
53
+ "standard-version": "^9.5.0",
54
+ "vuepress": "next"
55
+ },
56
+ "peerDependencies": {
57
+ "monaco-editor": "*"
58
+ },
59
+ "remarkConfig": {
60
+ "plugins": [
61
+ "remark-preset-lint-recommended",
62
+ [
63
+ "remark-lint-list-item-indent",
64
+ "mixed"
65
+ ]
66
+ ]
67
+ },
68
+ "packageManager": "yarn@3.2.2"
69
+ }