fei-editor
Version:
基于vue的代码编辑器插件
305 lines (304 loc) • 8.13 kB
JavaScript
import { defineComponent, ref, computed, watch, nextTick, onBeforeUnmount, onMounted, openBlock, createElementBlock, normalizeStyle } from "vue";
import * as ace from "brace";
const _sfc_main = /* @__PURE__ */ defineComponent({
...{
name: "FeiEditor"
},
__name: "index",
props: {
modelValue: {},
lang: { default: "json" },
theme: { default: "chrome" },
height: { default: "350" },
width: { default: "100%" },
fontSize: { default: 12 },
options: {},
readonly: { type: Boolean, default: false },
wrap: { type: Boolean, default: false },
snippets: { type: Boolean, default: true },
styles: {}
},
emits: ["blur", "focus", "init", "update:modelValue", "change"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emit = __emit;
const editorRef = ref(null);
const editor = ref(null);
const contentBackup = ref("");
const wrapStyles = computed(() => {
return {
width: props.width ? px(props.width) : "100%",
height: props.height ? px(props.height) : "100%",
border: "1px solid #e8e8e8",
...props.styles
};
});
const px = (n) => {
return /^\d*$/.test(n) ? `${n}px` : n;
};
const getValue = () => editor.value?.getValue() || "";
const setValue = (value, cursor) => {
if (!editor.value) return;
const next = value || "";
editor.value.setValue(next, cursor === void 0 ? next.length : cursor);
contentBackup.value = next;
};
const setTheme = (theme) => {
editor.value?.setTheme("ace/theme/" + theme);
};
const setLang = (lang) => {
editor.value?.getSession().setMode("ace/mode/" + lang);
};
const focus = () => {
editor.value?.focus();
};
const blur = () => {
editor.value?.blur();
};
const getEditor = () => editor.value;
const handleFocus = (event) => {
emit("focus", event);
};
const handleBlur = (event) => {
emit("blur", event);
};
watch(
() => props.modelValue,
(val) => {
if (contentBackup.value !== val && editor.value) {
const next = val || "";
editor.value.getSession().setValue(next, next.length);
contentBackup.value = next;
}
}
);
watch(
() => props.theme,
(newTheme) => {
if (editor.value) {
editor.value.setTheme("ace/theme/" + newTheme);
}
}
);
watch(
() => props.lang,
(newLang) => {
if (editor.value) {
editor.value.getSession().setMode("ace/mode/" + newLang);
}
}
);
watch(
() => props.options,
(newOption) => {
if (editor.value && newOption) {
editor.value.setOptions(newOption);
}
}
);
watch(
() => props.fontSize,
(newSize) => {
if (editor.value) {
editor.value.setFontSize(newSize);
}
}
);
watch(
() => props.readonly,
(val) => {
if (editor.value) {
editor.value.setReadOnly(val);
}
}
);
watch(
() => props.height,
() => {
nextTick(() => {
editor.value?.resize();
});
}
);
watch(
() => props.width,
() => {
nextTick(() => {
editor.value?.resize();
});
}
);
onBeforeUnmount(() => {
if (editor.value) {
editor.value.destroy();
editor.value = null;
}
});
onMounted(() => {
const editorElement = editorRef.value;
if (!editorElement) return;
const aceEditor = ace.edit(editorElement);
editor.value = aceEditor;
aceEditor.getSession().setMode("ace/mode/" + props.lang);
aceEditor.setTheme("ace/theme/" + props.theme);
aceEditor.$blockScrolling = Infinity;
aceEditor.setFontSize(props.fontSize);
aceEditor.setReadOnly(props.readonly);
aceEditor.getSession().setUseWrapMode(props.wrap);
aceEditor.setShowPrintMargin(false);
aceEditor.getSession().setTabSize(2);
aceEditor.setOptions({
enableBasicAutocompletion: props.snippets,
enableSnippets: props.snippets,
enableLiveAutocompletion: props.snippets
});
emit("init", aceEditor);
if (props.modelValue) {
aceEditor.setValue(props.modelValue, -1);
}
contentBackup.value = props.modelValue || "";
aceEditor.on("change", function() {
const content = aceEditor.getValue();
emit("update:modelValue", content);
emit("change", content);
contentBackup.value = content;
});
aceEditor.on("focus", handleFocus);
aceEditor.on("blur", handleBlur);
if (props.options) {
aceEditor.setOptions(props.options);
}
});
__expose({
getValue,
setValue,
setTheme,
setLang,
setMode: setLang,
setReadOnly: (readonly) => editor.value?.setReadOnly(readonly),
setFontSize: (size) => editor.value?.setFontSize(size),
resize: () => editor.value?.resize(),
destroy: () => {
editor.value?.destroy();
editor.value = null;
},
focus,
blur,
getEditor
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
ref_key: "editorRef",
ref: editorRef,
class: "fei-editor",
style: normalizeStyle(wrapStyles.value)
}, null, 4);
};
}
});
function typeColor(type = "default") {
let color = "";
switch (type) {
case "primary":
color = "#1089ff";
break;
case "success":
color = "#52c41a";
break;
case "warning":
color = "#fa8c16";
break;
case "danger":
color = "#f5222d";
break;
case "default":
color = "#35495E";
break;
default:
color = type;
break;
}
return color;
}
const print = (text, type = "default", back = false) => {
if (typeof text === "object") {
console.dir(text);
return;
}
if (back) {
console.log(
`%c ${text} `,
`background:${typeColor(type)}; padding: 2px; border-radius: 2px;color: #fff;`
);
} else {
console.log(`%c ${text} `, `color: ${typeColor(type)};`);
}
};
const pretty = (title, text, type = "primary") => {
console.log(
`%c ${title} %c ${text} %c`,
`background:${typeColor(type)};border:1px solid ${typeColor(
type
)}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
`border:1px solid ${typeColor(
type
)}; padding: 1px; border-radius: 0 2px 2px 0; color: ${typeColor(type)};`,
"background:transparent"
);
};
const primary = (text, back = false) => {
print(text, "primary", back);
};
const success = (text, back = false) => {
print(text, "success", back);
};
const info = (text, back = false) => {
print(text, "info", back);
};
const warning = (text, back = false) => {
print(text, "warning", back);
};
const danger = (text, back = false) => {
print(text, "danger", back);
};
const printVersion = (title, version2, link, type = "primary", fontSize = "20px", versionColor = "#999") => {
console.log(
`%c${title.toUpperCase()} %cV${version2} ${link}`,
`color: ${typeColor(
type
)};font-weight: 500;font-size: ${fontSize};font-family: Source Sans Pro,Helvetica Neue,Arial,sans-serif;`,
`color: ${versionColor};`
);
};
const log = {
print,
pretty,
primary,
info,
success,
warning,
danger,
printVersion
};
const name = "fei-editor";
const version$1 = "0.0.3";
const homepage = "https://ymf930.gitee.io/fei-editor/";
const config = {
name,
version: version$1,
homepage
};
const version = config.version;
const install = function(app, options = {}) {
app.component(_sfc_main.name || "FeiEditor", _sfc_main);
if (!options.disabledDoc) {
log.printVersion(config.name, config.version, config.homepage, "#722ed1");
}
return app;
};
const index = { version, install };
export {
_sfc_main as Editor,
index as default
};
//# sourceMappingURL=fei-editor.es.js.map