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 a0797f0 commit fafb859
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
更新:
- 支持选中、多选内容块更新(会更新子内容块)
- 支持全文格式化

说明:仅会对 p(段落)、b(文档块)、l(列表块)、h(标题块)内容块进行格式化。

规则:

- 中文使用全角符号
- 英文使用半角符号
- 清除文档块
- 清除空文档块
- 标准命名或自定义转换(如 mysql ➡️ MySQL,详细列表见 `standardName/config.txt`
- ……
- 中英文插入空格请使用官方的「优化排版」
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typography-go",
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"description": "中文排版优化",
"repository": "",
Expand Down
26 changes: 5 additions & 21 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@
"name": "typography-go",
"author": "养恐龙",
"url": "https://github.com/hqweay/typography-go",
"version": "0.0.8",
"version": "0.0.9",
"minAppVersion": "3.0.0",
"backends": [
"windows",
"linux",
"darwin",
"ios",
"android"
],
"frontends": [
"desktop",
"mobile",
"browser-desktop",
"browser-mobile",
"desktop-window"
],
"backends": ["windows", "linux", "darwin"],
"frontends": ["desktop", "desktop-window"],
"displayName": {
"en_US": "中文排版优化",
"zh_CN": "中文排版优化"
Expand All @@ -31,11 +19,7 @@
"zh_CN": "README.md"
},
"funding": {
"custom": [
"https://afdian.net/a/frostime"
]
"custom": ["https://afdian.net/a/frostime"]
},
"keywords": [
"plugin", "type", "中文"
]
"keywords": ["plugin", "type", "中文"]
}
Binary file modified preview.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 30 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class PluginSample extends Plugin {
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: "",
Expand All @@ -17,35 +18,35 @@ export default class PluginSample extends Plugin {
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,
""
// console.log(editElement);
editElement.textContent = formatUtil.formatContent(
editElement.textContent
);
doOperations.push({
id: item.dataset.nodeId,
data: item.outerHTML,
action: "update",
});

if (editElement.textContent) {
doOperations.push({
id: item.dataset.nodeId,
data: item.outerHTML,
action: "update",
});
// 若为空的话,删除该元素
} else {
editElement.remove();
doOperations.push({
id: item.dataset.nodeId,
action: "delete",
});
}
}
});
});
Expand All @@ -55,11 +56,12 @@ export default class PluginSample extends Plugin {
}
onload() {
this.eventBus.on("click-blockicon", this.blockIconEventBindThis);

this.addTopBar({
icon: this.myIcon,
title: this.i18n.addTopBarIcon,
position: "right",
callback: async () => {
callback: async (detail) => {
//寻找当前编辑的文档的id
let parentId = formatUtil.getDocid();
if (parentId == null) {
Expand All @@ -79,6 +81,14 @@ export default class PluginSample extends Plugin {
);
let childrenBlocks = childrenResult.data;

// const currentDoc = formatUtil.getCurrentDocument();

// const allEmements =
// currentDoc.querySelectorAll("[data-node-index]");

// //如果界面已经加载了文档所有内容,通过元素操作来更新
// if (allEmements && allEmements.length === childrenBlocks.length) {

for (let i = 0; i < childrenBlocks.length; i++) {
if (i === 0 || i % 50 === 0) {
showMessage(
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class FormatUtil {
)
?.getAttribute("data-node-id");
}
getCurrentDocument() {
return document.querySelector("[data-doc-type=NodeDocument]");
}

condenseContent(content: any) {
// 将 制表符 改成 四个空格
Expand Down

0 comments on commit fafb859

Please sign in to comment.