From 1024ae8943a3de2a94f3edc5adb9e13579b25a37 Mon Sep 17 00:00:00 2001 From: s4my Date: Fri, 12 Apr 2024 10:23:07 +0100 Subject: [PATCH] perf: removed a lot of unused imports, variables and functions. --- src/modals/cMenuToolbarModal.ts | 84 +++++++++++++-------------------- src/plugin/main.ts | 33 ++++++------- src/settings/settingsTab.ts | 35 +++++--------- src/util/fullscreen.ts | 10 ++-- 4 files changed, 63 insertions(+), 99 deletions(-) diff --git a/src/modals/cMenuToolbarModal.ts b/src/modals/cMenuToolbarModal.ts index 92abb0b..9d54cb2 100644 --- a/src/modals/cMenuToolbarModal.ts +++ b/src/modals/cMenuToolbarModal.ts @@ -1,6 +1,6 @@ import type cMenuToolbarPlugin from "src/plugin/main"; -import { App, Notice, debounce, requireApiVersion, ItemView,MarkdownView, ButtonComponent, WorkspaceParent, WorkspaceWindow, SettingTab, WorkspaceParentExt, View } from "obsidian"; +import { App, Notice, requireApiVersion, ItemView,MarkdownView, ButtonComponent, WorkspaceParent, WorkspaceWindow, WorkspaceParentExt } from "obsidian"; import { setBottomValue } from "src/util/statusBarConstants"; import { backcolorpicker, colorpicker } from "src/util/util"; import { t } from "src/translations/helper"; @@ -19,7 +19,7 @@ export function getRootSplits(): WorkspaceParentExt[] { // @ts-ignore floatingSplit is undocumented const floatingSplit = app.workspace.floatingSplit as WorkspaceParentExt; floatingSplit?.children.forEach((child: WorkspaceParentExt) => { - // if this is a window, push it to the list + // if this is a window, push it to the list if (child instanceof WorkspaceWindow) { rootSplits.push(child); } @@ -193,24 +193,24 @@ export function createTablecell(app: App, plugin: cMenuToolbarPlugin, el: string // console.log(backcolor,'backcolor') if (el == "x-color-picker-table") { plugin.settings.cMenuFontColor = backcolor; - setFontcolor(app, plugin, backcolor); + setFontcolor(app, backcolor); let font_colour_dom = activeDocument.querySelectorAll("#change-font-color-icon") font_colour_dom.forEach(element => { let ele = element as HTMLElement ele.style.fill = backcolor; }); - } else if (el == "x-backgroundcolor-picker-table") { + } else if (el == "x-backgroundcolor-picker-table") { plugin.settings.cMenuBackgroundColor = backcolor; //console.log("333") - setBackgroundcolor(app, plugin, backcolor); + setBackgroundcolor(app, backcolor); let background_colour_dom = activeDocument.querySelectorAll("#change-background-color-icon") background_colour_dom.forEach(element => { let ele = element as HTMLElement ele.style.fill = backcolor; }); - - + + // background_colour_dom.style.fill = plugin.settings.cMenuBackgroundColor; } plugin.saveSettings(); @@ -222,11 +222,8 @@ export function createTablecell(app: App, plugin: cMenuToolbarPlugin, el: string } } -export function setFontcolor(app: App, plugin: cMenuToolbarPlugin, color: string) { +export function setFontcolor(app: App, color: string) { //from https://github.com/obsidian-canzi/Enhanced-editing - const activeLeaf = app.workspace.getActiveViewOfType(MarkdownView); - - const view = activeLeaf; const editor = app.workspace.activeLeaf.view?.editor; let selectText = editor.getSelection(); // if (selectText == null || selectText.trim() == "") { @@ -261,14 +258,11 @@ export function setFontcolor(app: App, plugin: cMenuToolbarPlugin, color: string editor.exec("goRight"); // @ts-ignore app.commands.executeCommandById("editor:focus"); - + } -export function setBackgroundcolor(app: App, plugin: cMenuToolbarPlugin, color: string) { +export function setBackgroundcolor(app: App, color: string) { //from https://github.com/obsidian-canzi/Enhanced-editing - const activeLeaf = app.workspace.getActiveViewOfType(MarkdownView); - - const view = activeLeaf; const editor = app.workspace.activeLeaf.view?.editor; let selectText = editor.getSelection(); // console.log(selectText,'selectText') @@ -304,7 +298,7 @@ export function setBackgroundcolor(app: App, plugin: cMenuToolbarPlugin, color: editor.exec("goRight"); //@ts-ignore app.commands.executeCommandById("editor:focus"); - + } export const setcolorHex = function (color: string) { @@ -389,11 +383,7 @@ export function quiteFormatbrushes(plugin:cMenuToolbarPlugin) { export function setHeader(_str: string) { //from https://github.com/obsidian-canzi/Enhanced-editing - - const activeLeaf = app.workspace.getActiveViewOfType(MarkdownView); - - const view = activeLeaf; - const editor = app.workspace.activeLeaf.view?.editor;; + const editor = app.workspace.activeLeaf.view?.editor; let linetext = editor.getLine(editor.getCursor().line); let newstr, linend = ""; const regex = /^(\>*(\[[!\w]+\])?\s*)#+\s/; @@ -419,21 +409,11 @@ export function setHeader(_str: string) { }; editor.setLine(editor.getCursor().line, newstr); editor.setCursor({ line: editor.getCursor().line, ch: Number(newstr.length - linend.length) }); - + } export function setFormateraser(app: App, plugin: cMenuToolbarPlugin) { - - const editor = app.workspace.activeLeaf.view?.editor; - let linend; - let selstart = editor.getRange({ line: editor.getCursor().line, ch: 0 }, editor.getCursor()); - let linetext = editor.getLine(editor.getCursor().line); - if (linetext != "") { - linend = editor.getRange(editor.getCursor(), { line: editor.getCursor().line, ch: linetext.length }); - } else { - linend = editor.getRange(editor.getCursor(), { line: editor.getCursor().line, ch: 0 }); - }; let selectText = editor.getSelection(); if (selectText == null || selectText == "") { quiteFormatbrushes(plugin); @@ -464,14 +444,14 @@ export function setFormateraser(app: App, plugin: cMenuToolbarPlugin) { app.commands.executeCommandById("editor:focus"); } - } + export const createFollowingbar = (app: App, settings: cMenuToolbarSettings) => { let cMenuToolbarModalBar = isExistoolbar(app, settings); if (isSource(app)) { if (cMenuToolbarModalBar) { - const editor = app.workspace.getActiveViewOfType(MarkdownView).editor; + const editor = app.workspace.activeLeaf.view?.editor; cMenuToolbarModalBar.style.visibility = editor.somethingSelected() ? "visible" : "hidden"; cMenuToolbarModalBar.style.height = (settings.aestheticStyle === "tiny") ? 30 + "px" : 40 + "px"; @@ -521,6 +501,7 @@ export function cMenuToolbarPopover( function createMenu() { const generateMenu = () => { let btnwidth = 0; + let leafwidth = 0; let cMenuToolbar = createEl("div"); if (cMenuToolbar) { if (settings.positionStyle == "top") { @@ -531,7 +512,7 @@ export function cMenuToolbarPopover( }px;` ); cMenuToolbar.className += " top"; - if (settings.autohide) + if (settings.autohide) { cMenuToolbar.className += " autohide"; } @@ -571,10 +552,9 @@ export function cMenuToolbarPopover( // cMenuToolbar.style.visibility = "hidden"; // } - let leafwidth = 99999; if (settings.positionStyle == "top") { - let currentleaf = app.workspace.activeLeaf.view.containerEl - + let currentleaf = app.workspace.getActiveViewOfType(MarkdownView).containerEl; + if (!currentleaf?.querySelector("#cMenuToolbarPopoverBar")) { const markdownDom =currentleaf?.querySelector(".markdown-source-view"); @@ -602,13 +582,13 @@ export function cMenuToolbarPopover( - let cMenuToolbarPopoverBar = app.workspace.activeLeaf.view.containerEl + let cMenuToolbarPopoverBar = app.workspace.getActiveViewOfType(MarkdownView).containerEl ?.querySelector("#cMenuToolbarPopoverBar") as HTMLElement settings.menuCommands.forEach((item, index) => { - let tip + let tip; if ("SubmenuCommands" in item) { let _btn: any; - + if (btnwidth >= leafwidth - 26 * 4 && leafwidth > 100) { //说明已经溢出 plugin.setIS_MORE_Button(true); @@ -623,10 +603,10 @@ export function cMenuToolbarPopover( } else { - if(settings.positionStyle != "top") + if(settings.positionStyle != "top") _btn.buttonEl.setAttribute('aria-label-position','top') } - + checkHtml(item.icon) ? (_btn.buttonEl.innerHTML = item.icon) : _btn.setIcon(item.icon); @@ -661,7 +641,7 @@ export function cMenuToolbarPopover( }); if(index < settings.cMenuNumRows) { - if(settings.positionStyle != "top") + if(settings.positionStyle != "top") sub_btn.buttonEl.setAttribute('aria-label-position','top') } if (subitem.id == "cMenuToolbar-Divider-Line") @@ -736,7 +716,7 @@ export function cMenuToolbarPopover( let settingEI = app.setting.activeTab.containerEl.querySelector(".custom_font") if (settingEI) { settingEI.addClass?.("toolbar-cta"); } }, 200); - + }); } } else if (item.id == "editing-toolbar:change-background-color") { @@ -800,7 +780,7 @@ export function cMenuToolbarPopover( let settingEI = app.setting.activeTab.containerEl.querySelector(".custom_bg") if (settingEI) { settingEI.addClass?.("toolbar-cta"); } }, 200); - + }); } @@ -829,11 +809,11 @@ export function cMenuToolbarPopover( button.setClass("cMenuToolbarCommandItem"); if(index >= settings.cMenuNumRows) { - + button.setClass("cMenuToolbarSecond"); }else - { - if(settings.positionStyle != "top") + { + if(settings.positionStyle != "top") button.buttonEl.setAttribute('aria-label-position','top') } if (item.id == "cMenuToolbar-Divider-Line") @@ -880,7 +860,7 @@ export function cMenuToolbarPopover( } } - + createMenu(); } @@ -902,4 +882,4 @@ function setsvgColor(fontcolor: string, bgcolor: string) { }); } -} \ No newline at end of file +} diff --git a/src/plugin/main.ts b/src/plugin/main.ts index 68e9e11..161228b 100644 --- a/src/plugin/main.ts +++ b/src/plugin/main.ts @@ -8,14 +8,11 @@ import { Editor, MarkdownView, ItemView, - SliderComponent, ToggleComponent, - ButtonComponent, requireApiVersion, App, } from "obsidian"; import { wait } from "src/util/util"; -import { appIcons } from "src/icons/appIcons"; import { CommandPicker, openSlider } from "src/modals/suggesterModals"; import { cMenuToolbarSettingTab } from "src/settings/settingsTab"; import { selfDestruct, cMenuToolbarPopover, isSource, quiteFormatbrushes, setFontcolor, setBackgroundcolor, setHeader, createFollowingbar, setFormateraser, isExistoolbar, resetToolbar } from "src/modals/cMenuToolbarModal"; @@ -26,7 +23,7 @@ import addIcons, { // addBoxIcons } from "src/icons/customIcons"; -import { setMenuVisibility, setBottomValue } from "src/util/statusBarConstants"; +import { setMenuVisibility } from "src/util/statusBarConstants"; import { fullscreenMode, workplacefullscreenMode } from "src/util/fullscreen"; import { t } from "src/translations/helper"; @@ -231,7 +228,7 @@ export default class cMenuToolbarPlugin extends Plugin { //let cmEditor = view.sourceMode.cmEditor; - let cmEditor = this.app.workspace.activeLeaf.view?.editor; + let cmEditor = app.workspace.activeLeaf.view?.editor; if (cmEditor.hasFocus()) { let cMenuToolbarModalBar = isExistoolbar(this.app, this.settings) @@ -242,9 +239,9 @@ export default class cMenuToolbarPlugin extends Plugin { } else { // console.log(this.EN_FontColor_Format_Brush,'EN_FontColor_Format_Brush') if (this.EN_FontColor_Format_Brush) { - setFontcolor(this.app, this, this.settings.cMenuFontColor); + setFontcolor(this.app, this.settings.cMenuFontColor); } else if (this.EN_BG_Format_Brush) { - setBackgroundcolor(this.app, this, this.settings.cMenuBackgroundColor); + setBackgroundcolor(this.app, this.settings.cMenuBackgroundColor); } else if (this.EN_Text_Format_Brush) { setFormateraser(this.app, this); } else if (this.settings.positionStyle == "following") { @@ -302,14 +299,14 @@ export default class cMenuToolbarPlugin extends Plugin { this.addCommand({ id: 'change-font-color', name: 'Change font color[html]', - callback: () => setFontcolor(app, this, this.settings.cMenuFontColor ?? "#2DC26B"), + callback: () => setFontcolor(app, this.settings.cMenuFontColor ?? "#2DC26B"), icon: `` }); this.addCommand({ id: 'change-background-color', name: 'Change Backgroundcolor[html]', - callback: () => setBackgroundcolor(app, this, this.settings.cMenuBackgroundColor ?? "#FA541C"), + callback: () => setBackgroundcolor(app, this.settings.cMenuBackgroundColor ?? "#FA541C"), icon: `` }); @@ -319,7 +316,7 @@ export default class cMenuToolbarPlugin extends Plugin { callback: () => { //const activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView); //const view = activeLeaf; - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; //@ts-ignore return editor.indentList(); }, @@ -330,7 +327,7 @@ export default class cMenuToolbarPlugin extends Plugin { id: 'undent-list', name: 'unindent-list', callback: () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; //@ts-ignore return editor.unindentList(); }, @@ -341,7 +338,7 @@ export default class cMenuToolbarPlugin extends Plugin { id: 'editor-undo', name: 'undo editor', callback: () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; return editor.undo(); }, icon: "undo-glyph" @@ -353,7 +350,7 @@ export default class cMenuToolbarPlugin extends Plugin { callback: () => { const activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView); const view = activeLeaf; - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; return editor.redo(); }, icon: "redo-glyph" @@ -364,7 +361,7 @@ export default class cMenuToolbarPlugin extends Plugin { name: 'copy editor', callback: async () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; try { await window.navigator.clipboard.writeText(editor.getSelection()); // 使用 window.navigator.clipboard.writeText() 方法将选定的文本写入剪贴板 app.commands.executeCommandById("editor:focus"); @@ -381,7 +378,7 @@ export default class cMenuToolbarPlugin extends Plugin { callback: async () => { const activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView); const view = activeLeaf; - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; try { var replaceSelection = editor.replaceSelection; // 获取编辑器的替换选区方法 var text = await window.navigator.clipboard.readText(); // 使用 window.navigator.clipboard.readText() 方法读取剪贴板中的文本 @@ -399,7 +396,7 @@ export default class cMenuToolbarPlugin extends Plugin { name: 'cut editor', callback: async () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; try { await window.navigator.clipboard.writeText(editor.getSelection()); // 使用 window.navigator.clipboard.writeText() 方法将选定的文本写入剪贴板 editor.replaceSelection(""); // 清空选定的文本 @@ -594,7 +591,7 @@ export default class cMenuToolbarPlugin extends Plugin { name: `Toggle ${type}`, icon: `${type}-glyph`, callback: async () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; applyCommand(commandsMap[type], editor); await wait(10); //@ts-ignore @@ -610,7 +607,7 @@ export default class cMenuToolbarPlugin extends Plugin { name: `${type["name"]}`, icon: `${type["icon"]}`, callback: async () => { - const editor = this.app.workspace.activeLeaf.view?.editor; + const editor = app.workspace.activeLeaf.view?.editor; editor.getCursor("from"); const curserEnd = editor.getCursor("to"); let char; diff --git a/src/settings/settingsTab.ts b/src/settings/settingsTab.ts index b6ac992..58db47a 100644 --- a/src/settings/settingsTab.ts +++ b/src/settings/settingsTab.ts @@ -9,7 +9,6 @@ import { GenNonDuplicateID } from "src/util/util"; import { t } from 'src/translations/helper'; import Pickr from "@simonwep/pickr"; -import { settings } from "cluster"; function getPickrSettings(opts: { @@ -63,16 +62,6 @@ export function getComandindex(item: any, arr: any[]): number { return idx; } -function getComandindex2(array: any[]): number { - for (let i = 0; i < array.length; i++) { - const settingItem = array[i].querySelector('.setting-item'); - - if (settingItem && settingItem.dataset["id"]) { - return i; - } - } - return -1; -} export class cMenuToolbarSettingTab extends PluginSettingTab { plugin: cMenuToolbarPlugin; appendMethod: string; @@ -198,7 +187,7 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { }); } - + new Setting(containerEl) .setName(t('Mobile enabled or not') ) @@ -428,9 +417,7 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { forceFallback: true, fallbackClass: "sortable-fallback", easing: "cubic-bezier(1, 0, 0, 1)", - onStart: function (evt) { - // console.log(evt); - }, + onStart: function () {}, onSort: (command) => { @@ -451,15 +438,15 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { const arrayResult = this.plugin.settings.menuCommands; let cmdindex = getComandindex(command.target.parentElement.dataset["id"], arrayResult); - + const subresult = arrayResult[cmdindex]?.SubmenuCommands; if (subresult) { - + const [removed] = subresult.splice(command.oldIndex, 1); arrayResult.splice(command.newIndex, 0, removed); this.plugin.saveSettings(); - + } else { console.error('Subresult is undefined.'); } @@ -479,11 +466,11 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { if (subresult) { - + const [removed] = arrayResult.splice(command.oldIndex, 1); subresult.splice(command.newIndex, 0, removed); this.plugin.saveSettings(); - + } else { console.error('Subresult is undefined.'); } @@ -539,7 +526,7 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { }); }); subsetting.nameEl; - + }); } else { setting @@ -624,9 +611,9 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { // setting.nameEl; }); - - + + } hide(): void { setTimeout(() => { @@ -636,6 +623,6 @@ export class cMenuToolbarSettingTab extends PluginSettingTab { } } - + diff --git a/src/util/fullscreen.ts b/src/util/fullscreen.ts index d18eb29..2cadbfa 100644 --- a/src/util/fullscreen.ts +++ b/src/util/fullscreen.ts @@ -6,9 +6,9 @@ export function workplacefullscreenMode(app: App) { if (app.workspace.leftSplit.collapsed && app.workspace.rightSplit.collapsed) { - //@ts-ignore + //@ts-ignore app.commands.executeCommandById("app:toggle-right-sidebar"); - //@ts-ignore + //@ts-ignore app.commands.executeCommandById("app:toggle-left-sidebar"); app.workspace.leftRibbon.show() @@ -26,12 +26,12 @@ export function workplacefullscreenMode(app: App) { } app.workspace.leftRibbon.hide() if (!app.workspace.leftSplit.collapsed) { - //@ts-ignore + //@ts-ignore app.commands.executeCommandById("app:toggle-left-sidebar"); } if (!app.workspace.rightSplit.collapsed) { - //@ts-ignore + //@ts-ignore app.commands.executeCommandById("app:toggle-right-sidebar"); } } @@ -100,7 +100,7 @@ export function fullscreenMode(app: App) { }); }); - modroot.addEventListener("fullscreenchange", function (event: any) { + modroot.addEventListener("fullscreenchange", function () { if (!isFull(modroot)) { fullscreenMutationObserver.disconnect(); }