Skip to content

Commit

Permalink
feat#格式化单内容块
Browse files Browse the repository at this point in the history
  • Loading branch information
hqweay committed Apr 3, 2024
1 parent 21a94b6 commit a0797f0
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,60 @@ import { Plugin, showMessage, confirm, fetchSyncPost } from "siyuan";
import { formatUtil } from "./utils";

export default class PluginSample extends Plugin {
private blockIconEventBindThis = this.blockIconEvent.bind(this);

myIcon = `<svg t="1711874745467" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1408" width="32" height="32"><path d="M28.668 367.46H114.8v36H28.668z" fill="#4A4A4A" p-id="1409"></path><path d="M904.944 637.676c0 72.088-58.976 131.068-131.064 131.068H245.864c-72.088 0-131.068-58.976-131.068-131.068V186.984c0-72.088 58.98-131.068 131.068-131.068h528.02c72.084 0 131.064 58.98 131.064 131.068v450.692z" fill="#B9B9BF" p-id="1410"></path><path d="M773.884 786.744H245.864c-82.196 0-149.068-66.868-149.068-149.068V186.984c0-82.196 66.872-149.068 149.068-149.068h528.02c82.196 0 149.072 66.872 149.072 149.068v450.692c-0.012 82.2-66.876 149.068-149.072 149.068zM245.864 73.916c-62.344 0-113.068 50.724-113.068 113.068v450.692c0 62.344 50.724 113.068 113.068 113.068h528.02c62.344 0 113.072-50.724 113.072-113.068V186.984c0-62.344-50.728-113.068-113.072-113.068H245.864z" fill="#4A4A4A" p-id="1411"></path><path d="M337.612 292.892m-121.704 0a121.704 121.704 0 1 0 243.408 0 121.704 121.704 0 1 0-243.408 0Z" fill="#94E5FF" p-id="1412"></path><path d="M337.612 432.596c-77.032 0-139.704-62.672-139.704-139.708 0-77.032 62.672-139.704 139.704-139.704 77.036 0 139.708 62.672 139.708 139.704 0 77.036-62.672 139.708-139.708 139.708z m0-243.412c-57.184 0-103.704 46.52-103.704 103.704s46.52 103.708 103.704 103.708c57.184 0 103.708-46.524 103.708-103.708S394.796 189.184 337.612 189.184z" fill="#4A4A4A" p-id="1413"></path><path d="M683.548 292.892m-121.704 0a121.704 121.704 0 1 0 243.408 0 121.704 121.704 0 1 0-243.408 0Z" fill="#94E5FF" p-id="1414"></path><path d="M683.548 432.596c-77.036 0-139.708-62.672-139.708-139.708 0-77.032 62.672-139.704 139.708-139.704 77.032 0 139.704 62.672 139.704 139.704 0 77.036-62.672 139.708-139.704 139.708z m0-243.412c-57.188 0-103.708 46.52-103.708 103.704s46.52 103.708 103.708 103.708c57.18 0 103.704-46.524 103.704-103.708s-46.524-103.704-103.704-103.704zM901.204 367.46h86.128v36h-86.128z" fill="#4A4A4A" p-id="1415"></path><path d="M396.168 986.668v-217.924h220.94v217.924" fill="#8A8A8A" p-id="1416"></path><path d="M337.612 546.484h345.936v104.856H337.612z" fill="#8A8A8A" p-id="1417"></path><path d="M683.548 669.34H337.612a18 18 0 0 1-18-18v-104.856c0-9.936 8.06-18 18-18h345.936c9.936 0 18 8.064 18 18v104.856c0 9.94-8.064 18-18 18z m-327.936-36h309.936v-68.856H355.612v68.856z" fill="#4A4A4A" p-id="1418"></path><path d="M575.408 669.34a18.004 18.004 0 0 1-18-18v-104.856a18.004 18.004 0 0 1 36 0v104.856a18 18 0 0 1-18 18zM448.084 669.34a18 18 0 0 1-18-18v-104.856a18.004 18.004 0 0 1 36 0v104.856a18 18 0 0 1-18 18zM396.168 822.928h220.94v36H396.168zM396.168 895.512h220.94v36H396.168z" fill="#4A4A4A" p-id="1419"></path><path d="M338 298m-32 0a32 32 0 1 0 64 0 32 32 0 1 0-64 0Z" fill="#4A4A4A" p-id="1420"></path><path d="M683.548 298m-32 0a32 32 0 1 0 64 0 32 32 0 1 0-64 0Z" fill="#4A4A4A" p-id="1421"></path></svg>`;
// availableBlocks = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"];
availableBlocks = ["NodeParagraph", "NodeHeading"];
private blockIconEvent({ detail }: any) {
detail.menu.addItem({
iconHTML: "",
// label: this.i18n.removeSpace,
label: this.i18n.addTopBarIcon,
click: () => {
const doOperations: IOperation[] = [];

detail.blockElements.forEach((item: HTMLElement) => {
console.log("--111--");
console.log(item);
console.log("--111--");

const editElements = item.querySelectorAll(
this.availableBlocks
.map((item) => {
// return `.${item} [contenteditable="true"]`;
return `[data-type=${item}] [contenteditable="true"]`;
})
.join(",")
);
// const editElement = item.querySelector(
// '[contenteditable="true"], [contenteditable="false"]'
// );
console.log("----");
console.log(editElements);
editElements.forEach((editElement: HTMLElement) => {
if (editElement) {
console.log(editElement);
editElement.textContent = editElement.textContent.replace(
/ /g,
""
);
doOperations.push({
id: item.dataset.nodeId,
data: item.outerHTML,
action: "update",
});
}
});
});
detail.protyle.getInstance().transaction(doOperations);
},
});
}
onload() {
this.eventBus.on("click-blockicon", this.blockIconEventBindThis);
this.addTopBar({
icon: `<svg t="1711874745467" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1408" width="32" height="32"><path d="M28.668 367.46H114.8v36H28.668z" fill="#4A4A4A" p-id="1409"></path><path d="M904.944 637.676c0 72.088-58.976 131.068-131.064 131.068H245.864c-72.088 0-131.068-58.976-131.068-131.068V186.984c0-72.088 58.98-131.068 131.068-131.068h528.02c72.084 0 131.064 58.98 131.064 131.068v450.692z" fill="#B9B9BF" p-id="1410"></path><path d="M773.884 786.744H245.864c-82.196 0-149.068-66.868-149.068-149.068V186.984c0-82.196 66.872-149.068 149.068-149.068h528.02c82.196 0 149.072 66.872 149.072 149.068v450.692c-0.012 82.2-66.876 149.068-149.072 149.068zM245.864 73.916c-62.344 0-113.068 50.724-113.068 113.068v450.692c0 62.344 50.724 113.068 113.068 113.068h528.02c62.344 0 113.072-50.724 113.072-113.068V186.984c0-62.344-50.728-113.068-113.072-113.068H245.864z" fill="#4A4A4A" p-id="1411"></path><path d="M337.612 292.892m-121.704 0a121.704 121.704 0 1 0 243.408 0 121.704 121.704 0 1 0-243.408 0Z" fill="#94E5FF" p-id="1412"></path><path d="M337.612 432.596c-77.032 0-139.704-62.672-139.704-139.708 0-77.032 62.672-139.704 139.704-139.704 77.036 0 139.708 62.672 139.708 139.704 0 77.036-62.672 139.708-139.708 139.708z m0-243.412c-57.184 0-103.704 46.52-103.704 103.704s46.52 103.708 103.704 103.708c57.184 0 103.708-46.524 103.708-103.708S394.796 189.184 337.612 189.184z" fill="#4A4A4A" p-id="1413"></path><path d="M683.548 292.892m-121.704 0a121.704 121.704 0 1 0 243.408 0 121.704 121.704 0 1 0-243.408 0Z" fill="#94E5FF" p-id="1414"></path><path d="M683.548 432.596c-77.036 0-139.708-62.672-139.708-139.708 0-77.032 62.672-139.704 139.708-139.704 77.032 0 139.704 62.672 139.704 139.704 0 77.036-62.672 139.708-139.704 139.708z m0-243.412c-57.188 0-103.708 46.52-103.708 103.704s46.52 103.708 103.708 103.708c57.18 0 103.704-46.524 103.704-103.708s-46.524-103.704-103.704-103.704zM901.204 367.46h86.128v36h-86.128z" fill="#4A4A4A" p-id="1415"></path><path d="M396.168 986.668v-217.924h220.94v217.924" fill="#8A8A8A" p-id="1416"></path><path d="M337.612 546.484h345.936v104.856H337.612z" fill="#8A8A8A" p-id="1417"></path><path d="M683.548 669.34H337.612a18 18 0 0 1-18-18v-104.856c0-9.936 8.06-18 18-18h345.936c9.936 0 18 8.064 18 18v104.856c0 9.94-8.064 18-18 18z m-327.936-36h309.936v-68.856H355.612v68.856z" fill="#4A4A4A" p-id="1418"></path><path d="M575.408 669.34a18.004 18.004 0 0 1-18-18v-104.856a18.004 18.004 0 0 1 36 0v104.856a18 18 0 0 1-18 18zM448.084 669.34a18 18 0 0 1-18-18v-104.856a18.004 18.004 0 0 1 36 0v104.856a18 18 0 0 1-18 18zM396.168 822.928h220.94v36H396.168zM396.168 895.512h220.94v36H396.168z" fill="#4A4A4A" p-id="1419"></path><path d="M338 298m-32 0a32 32 0 1 0 64 0 32 32 0 1 0-64 0Z" fill="#4A4A4A" p-id="1420"></path><path d="M683.548 298m-32 0a32 32 0 1 0 64 0 32 32 0 1 0-64 0Z" fill="#4A4A4A" p-id="1421"></path></svg>`,
icon: this.myIcon,
title: this.i18n.addTopBarIcon,
position: "right",
callback: async () => {
Expand Down

0 comments on commit a0797f0

Please sign in to comment.