Skip to content

Commit 8be20e0

Browse files
committed
update typescript
1 parent c2d3812 commit 8be20e0

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

src/index.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
IModel,
1212
Setting,
1313
fetchPost,
14-
Protyle, openWindow
14+
Protyle, openWindow, IOperation
1515
} from "siyuan";
1616
import "@/index.scss";
1717

@@ -68,7 +68,7 @@ export default class PluginSample extends Plugin {
6868
});
6969

7070
const statusIconTemp = document.createElement("template");
71-
statusIconTemp.innerHTML = `<div class="toolbar__item b3-tooltips b3-tooltips__w" aria-label="Remove plugin-sample Data">
71+
statusIconTemp.innerHTML = `<div class="toolbar__item ariaLabel" aria-label="Remove plugin-sample Data">
7272
<svg>
7373
<use xlink:href="#iconTrashcan"></use>
7474
</svg>
@@ -272,19 +272,38 @@ export default class PluginSample extends Plugin {
272272
});
273273
}
274274

275+
private eventBusPaste(event: any) {
276+
// 如果需异步处理请调用 preventDefault, 否则会进行默认处理
277+
event.preventDefault();
278+
// 如果使用了 preventDefault,必须调用 resolve,否则程序会卡死
279+
event.detail.resolve({
280+
textPlain: event.detail.textPlain.trim(),
281+
});
282+
}
283+
275284
private eventBusLog({ detail }: any) {
276285
console.log(detail);
277286
}
278287

279288
private blockIconEvent({ detail }: any) {
280-
const ids: string[] = [];
281-
detail.blockElements.forEach((item: HTMLElement) => {
282-
ids.push(item.getAttribute("data-node-id"));
283-
});
284289
detail.menu.addItem({
285290
iconHTML: "",
286-
type: "readonly",
287-
label: "IDs<br>" + ids.join("<br>"),
291+
label: this.i18n.removeSpace,
292+
click: () => {
293+
const doOperations: IOperation[] = [];
294+
detail.blockElements.forEach((item: HTMLElement) => {
295+
const editElement = item.querySelector('[contenteditable="true"]');
296+
if (editElement) {
297+
editElement.textContent = editElement.textContent.replace(/ /g, "");
298+
doOperations.push({
299+
id: item.dataset.nodeId,
300+
data: item.outerHTML,
301+
action: "update"
302+
});
303+
}
304+
});
305+
detail.protyle.getInstance().transaction(doOperations);
306+
}
288307
});
289308
}
290309

@@ -488,9 +507,15 @@ export default class PluginSample extends Plugin {
488507
}
489508
}, {
490509
icon: "iconSelect",
491-
label: "On loaded-protyle",
510+
label: "On loaded-protyle-static",
511+
click: () => {
512+
this.eventBus.on("loaded-protyle-static", this.eventBusLog);
513+
}
514+
}, {
515+
icon: "iconClose",
516+
label: "Off loaded-protyle-static",
492517
click: () => {
493-
this.eventBus.on("loaded-protyle", this.eventBusLog);
518+
this.eventBus.off("loaded-protyle-static", this.eventBusLog);
494519
}
495520
}, {
496521
icon: "iconSelect",
@@ -516,11 +541,17 @@ export default class PluginSample extends Plugin {
516541
click: () => {
517542
this.eventBus.off("destroy-protyle", this.eventBusLog);
518543
}
544+
}, {
545+
icon: "iconSelect",
546+
label: "On open-menu-doctree",
547+
click: () => {
548+
this.eventBus.on("open-menu-doctree", this.eventBusLog);
549+
}
519550
}, {
520551
icon: "iconClose",
521-
label: "Off loaded-protyle",
552+
label: "Off open-menu-doctree",
522553
click: () => {
523-
this.eventBus.off("loaded-protyle", this.eventBusLog);
554+
this.eventBus.off("open-menu-doctree", this.eventBusLog);
524555
}
525556
}, {
526557
icon: "iconSelect",
@@ -630,6 +661,18 @@ export default class PluginSample extends Plugin {
630661
click: () => {
631662
this.eventBus.off("input-search", this.eventBusLog);
632663
}
664+
}, {
665+
icon: "iconSelect",
666+
label: "On paste",
667+
click: () => {
668+
this.eventBus.on("paste", this.eventBusPaste);
669+
}
670+
}, {
671+
icon: "iconClose",
672+
label: "Off paste",
673+
click: () => {
674+
this.eventBus.off("paste", this.eventBusPaste);
675+
}
633676
}, {
634677
icon: "iconSelect",
635678
label: "On open-siyuan-url-plugin",

0 commit comments

Comments
 (0)