Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Achuan-2 authored Oct 11, 2024
1 parent 921c775 commit 56a28ca
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import {
} from "siyuan";


const STORAGE_NAME = "menu-config";

export default class PluginSample extends Plugin {
private isMobile: boolean;
private formatPainterEnable = false;
private formatData: { datatype: string, style: string } | null = null;
private protyle: IProtyle;
onload() {
this.data[STORAGE_NAME] = { readonlyText: "Readonly" };
this.protyleOptions = {
toolbar: ["block-ref",
"a",
Expand Down Expand Up @@ -46,7 +44,7 @@ export default class PluginSample extends Plugin {
tip: this.i18n.tips,
click: (protyle: Protyle) => {
this.protyle = protyle.protyle;

if (!this.formatPainterEnable) {
const selectedInfo = getSelectedParentHtml();
if (selectedInfo) {
Expand All @@ -61,7 +59,7 @@ export default class PluginSample extends Plugin {
// console.log("选中无样式文字");
}
this.formatPainterEnable = true;
document.body.dataset.formatPainterEnable ="true";
document.body.dataset.formatPainterEnable = "true";
// console.log(this.formatData);
fetchPost("/api/notification/pushErrMsg", { "msg": this.i18n.enable, "timeout": 7000 });

Expand All @@ -72,11 +70,10 @@ export default class PluginSample extends Plugin {
}
///^ dock indicator worker

this.protyle.toolbar.range.collapse(true);
// 关闭toolbar
this.protyle.toolbar.range.collapse(true);
// 选择所有具有 .protyle-toolbar 类的元素
const toolbarElements = document.querySelectorAll('.protyle-toolbar');

// 遍历选中的元素
toolbarElements.forEach(element => {
// 检查元素是否没有 .fn__none 类
Expand All @@ -102,14 +99,14 @@ export default class PluginSample extends Plugin {
const selectedText = range.toString();
if (selectedText) {
const startBlockElement = hasClosestBlock(range.startContainer);
if ( range.endContainer.nodeType !== 3 && (range.endContainer as HTMLElement).tagName === "DIV" && range.endOffset === 0) {
if (range.endContainer.nodeType !== 3 && (range.endContainer as HTMLElement).tagName === "DIV" && range.endOffset === 0) {
// 三选中段落块时,rangeEnd 会在下一个块
if ((range.endContainer as HTMLElement).classList.contains("protyle-attr") && startBlockElement) {
// 三击在悬浮层中会选择到 attr https://github.com/siyuan-note/siyuan/issues/4636
// 需要获取可编辑元素,使用 previousElementSibling 的话会 https://github.com/siyuan-note/siyuan/issues/9714
setLastNodeRange(getContenteditableElement(startBlockElement), range, false);
}
}
}
this.protyle.toolbar.range = range; // 更改选区
// console.log(this.protyle.toolbar.range.toString());
// Apply the stored format to the selected text
Expand All @@ -131,26 +128,22 @@ export default class PluginSample extends Plugin {
type: "inline-math",
});
hasmath = true;
}
}
const otherTypes = this.formatData.datatype.replace(/\b(inline-math|block-ref|a|text)\b/g, "").trim();
if (otherTypes) {
this.protyle.toolbar.setInlineMark(this.protyle, otherTypes, "range");
this.protyle.toolbar.setInlineMark(this.protyle, "text", "range", {
"type": "color",
"color": "#FF0000"
});
}
}
if (this.formatData.style) {
// this.protyle.toolbar.setInlineMark(this.protyle, "text", "range", { "type": "style1", "color": this.formatData.style });
// console.log(backgroundColor, color, fontSize, textShadow);
let type = "text";
let type = "text";
if (hasmath) {
// 数学公式加颜色有bug
type = "inline-math";
return;
}
const { backgroundColor, color, fontSize, textShadow, webkitTextStroke, webkitTextFillColor} = parseStyle(this.formatData.style);
}
const { backgroundColor, color, fontSize, textShadow, webkitTextStroke, webkitTextFillColor } = parseStyle(this.formatData.style);
if (backgroundColor) {
this.protyle.toolbar.setInlineMark(this.protyle, type, "range", {
"type": "backgroundColor",
Expand All @@ -174,7 +167,7 @@ export default class PluginSample extends Plugin {
if (textShadow) {
this.protyle.toolbar.setInlineMark(this.protyle, type, "range", {
"type": "style4", //投影效果
"color": textShadow
"color": textShadow
});
}
if (webkitTextStroke) {
Expand All @@ -198,7 +191,6 @@ export default class PluginSample extends Plugin {
this.formatPainterEnable = false;
document.body.dataset.formatPainterEnable = "false";
this.formatData = null;
document.body.style.cursor = "auto"; // 恢复默认光标
fetchPost("/api/notification/pushMsg", { "msg": this.i18n.disable, "timeout": 7000 });

///v dock indicator worker
Expand Down Expand Up @@ -354,10 +346,12 @@ export default class PluginSample extends Plugin {
}

toggleFormatPainter() {
if (this.formatPainterEnable){
if (this.formatPainterEnable) {
fetchPost("/api/notification/pushMsg", { "msg": this.i18n.disable, "timeout": 7000 });
}else{
document.body.dataset.formatPainterEnable = "false";
} else {
fetchPost("/api/notification/pushErrMsg", { "msg": this.i18n.enable, "timeout": 7000 });
document.body.dataset.formatPainterEnable = "true";
}
this.formatPainterEnable = !this.formatPainterEnable;
const indicator = document.querySelector(".siyuan-plugin-formatPainter_brush_indicator");
Expand Down

0 comments on commit 56a28ca

Please sign in to comment.