|
|
@@ -38,23 +38,13 @@ export class Preview {
|
|
|
event.preventDefault();
|
|
|
});
|
|
|
|
|
|
- const actions = this.genActions(vditor.options.preview.actions);
|
|
|
- // 如果只有一个选项时也没必要呈现
|
|
|
- if (actions.length <= 1) {
|
|
|
- this.element.appendChild(previewElement);
|
|
|
- return ;
|
|
|
- }
|
|
|
-
|
|
|
+ const actions = vditor.options.preview.actions;
|
|
|
const actionElement = document.createElement("div");
|
|
|
actionElement.className = "vditor-preview__action";
|
|
|
const actionHtml: string[] = [];
|
|
|
- const actionCustom: IPreviewActionCustom[] = [];
|
|
|
- const customPrefix = `_custom-`;
|
|
|
for (let i = 0; i < actions.length; i++) {
|
|
|
const action = actions[i];
|
|
|
if (typeof action === "object") {
|
|
|
- action.key = customPrefix + (action.key || `${i + 1}`);
|
|
|
- actionCustom.push(action);
|
|
|
actionHtml.push(`
|
|
|
<button data-type="${action.key}" class="${action.className}">${action.text}</button>`);
|
|
|
continue;
|
|
|
@@ -77,7 +67,7 @@ export class Preview {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- actionElement.innerHTML = actionHtml.join(``);
|
|
|
+ actionElement.innerHTML = actionHtml.join("");
|
|
|
this.element.appendChild(actionElement);
|
|
|
this.element.appendChild(previewElement);
|
|
|
|
|
|
@@ -86,17 +76,13 @@ export class Preview {
|
|
|
if (!btn) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
const type = btn.getAttribute("data-type");
|
|
|
- if (type === actionElement.querySelector(".vditor-preview__action--current").getAttribute("data-type")) {
|
|
|
+ const actionCustom = actions.find((w: IPreviewActionCustom) => w?.key === type) as IPreviewActionCustom;
|
|
|
+ if (actionCustom) {
|
|
|
+ actionCustom.click(type);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (type.startsWith(customPrefix)) {
|
|
|
- actionCustom.find((w: IPreviewActionCustom) => w.key === type).click();
|
|
|
- return ;
|
|
|
- }
|
|
|
-
|
|
|
if (type === "mp-wechat" || type === "zhihu") {
|
|
|
this.copyToX(vditor, this.element.lastElementChild.cloneNode(true) as HTMLElement, type);
|
|
|
return;
|
|
|
@@ -238,13 +224,4 @@ export class Preview {
|
|
|
this.element.lastElementChild.remove();
|
|
|
vditor.tip.show(`已复制,可到${type === "zhihu" ? "知乎" : "微信公众号平台"}进行粘贴`);
|
|
|
}
|
|
|
-
|
|
|
- private genActions(actions: IPreviewActionType): Array<IPreviewAction| IPreviewActionCustom> {
|
|
|
- if (actions === "none") {
|
|
|
- return [];
|
|
|
- } else if (actions === "all") {
|
|
|
- return ["desktop" , "tablet" , "mobile" , "mp-wechat", "zhihu"];
|
|
|
- }
|
|
|
- return actions;
|
|
|
- }
|
|
|
}
|