nuxt-live-editor
Version:
My new Nuxt module
148 lines (142 loc) • 4.25 kB
JavaScript
import { createResolver, addComponent, defineNuxtModule, addPlugin, addComponentsDir, addImportsDir } from '@nuxt/kit';
import path from 'path';
function isPlainObject(value) {
if (value === null || typeof value !== "object") {
return false;
}
const prototype = Object.getPrototypeOf(value);
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
return false;
}
if (Symbol.iterator in value) {
return false;
}
if (Symbol.toStringTag in value) {
return Object.prototype.toString.call(value) === "[object Module]";
}
return true;
}
function _defu(baseObject, defaults, namespace = ".", merger) {
if (!isPlainObject(defaults)) {
return _defu(baseObject, {}, namespace, merger);
}
const object = Object.assign({}, defaults);
for (const key in baseObject) {
if (key === "__proto__" || key === "constructor") {
continue;
}
const value = baseObject[key];
if (value === null || value === void 0) {
continue;
}
if (merger && merger(object, key, value, namespace)) {
continue;
}
if (Array.isArray(value) && Array.isArray(object[key])) {
object[key] = [...value, ...object[key]];
} else if (isPlainObject(value) && isPlainObject(object[key])) {
object[key] = _defu(
value,
object[key],
(namespace ? `${namespace}.` : "") + key.toString(),
merger
);
} else {
object[key] = value;
}
}
return object;
}
function createDefu(merger) {
return (...arguments_) => (
// eslint-disable-next-line unicorn/no-array-reduce
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
);
}
const defu = createDefu();
const resolveComponents = () => {
const resolver = createResolver(import.meta.url);
const componentsDir = resolver.resolve("./runtime/components/live-editor");
const prefix = "V";
const allComponents = [
{
name: "Text",
path: "text.vue"
},
{
name: "Link",
path: "link.vue"
},
{
name: "Editor",
path: "index.vue"
},
{
name: "Content",
path: "content.vue"
},
{
name: "Icon",
path: "icon.vue"
},
{
name: "Image",
path: "image.vue"
},
{
name: "List",
path: "list.vue"
}
];
allComponents.forEach((component) => {
console.log(`Create components`, "V" + component.name);
addComponent({
name: prefix + component.name,
filePath: path.join(componentsDir, component.path)
});
});
};
const module = defineNuxtModule({
meta: {
name: "nuxt-live-editor",
configKey: "liveEditor"
},
defaults: {
color: {
live_primary: "#111827",
live_secondary: "#6366F1",
live_border: "#1e293b",
live_outline: "#B31B1B"
},
ai: {
token: "",
model: "",
temperature: 0.6
},
default_value: {
OPTIONS_TOOLBAR_FULL: "bold italic underline h1 h2 h3 h4 h5 h6 lineheight fontsizeinput image link forecolor backcolor alignleft aligncenter alignright alignjustify bullist blockquote undo redo table emoticons code",
OPTIONS_TOOLBAR_BASE: "bold italic underline link lineheight alignleft aligncenter alignright",
DEFAULT_CONTENT: "Write here...",
DEFAULT_CONTENT_BUTTON: "Button",
DEFAULT_CONTENT_IMAGE: "f1cd443c-3bfb-46c1-b731-cca496397b30",
DEFAULT_CONTENT_LINK: '{"url":"/","title":"","target":"default"}',
DEFAULT_CONTENT_ICON: ``
}
},
async setup(options, nuxt) {
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {};
nuxt.options.runtimeConfig.public.live_editor = defu(nuxt.options.runtimeConfig.public.live_editor, {
color: options.color,
ai: options.ai,
autoFetch: options.autoFetch,
default_value: options.default_value
});
const resolver = createResolver(import.meta.url);
addPlugin(resolver.resolve("./runtime/plugin"));
addComponentsDir({ path: resolver.resolve("./runtime/components") }).then();
addImportsDir(resolver.resolve("./runtime/components"));
addImportsDir(resolver.resolve("./runtime/stores"));
resolveComponents();
}
});
export { module as default };