Skip to content

Commit

Permalink
perf: removed a lot of unused imports, variables and functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
s4my committed Apr 14, 2024
1 parent 7179940 commit 1024ae8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 99 deletions.
84 changes: 32 additions & 52 deletions src/modals/cMenuToolbarModal.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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() == "") {
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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/;
Expand All @@ -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);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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") {
Expand All @@ -531,7 +512,7 @@ export function cMenuToolbarPopover(
}px;`
);
cMenuToolbar.className += " top";
if (settings.autohide)
if (settings.autohide)
{
cMenuToolbar.className += " autohide";
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -800,7 +780,7 @@ export function cMenuToolbarPopover(
let settingEI = app.setting.activeTab.containerEl.querySelector(".custom_bg")
if (settingEI) { settingEI.addClass?.("toolbar-cta"); }
}, 200);

});

}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -880,7 +860,7 @@ export function cMenuToolbarPopover(
}

}

createMenu();
}

Expand All @@ -902,4 +882,4 @@ function setsvgColor(fontcolor: string, bgcolor: string) {
});
}

}
}
33 changes: 15 additions & 18 deletions src/plugin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -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)

Expand All @@ -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") {
Expand Down Expand Up @@ -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: `<svg width="24" height="24" focusable="false" fill="currentColor"><g fill-rule="evenodd"><path id="change-font-color-icon" d="M3 18h18v3H3z" style="fill:#2DC26B"></path><path d="M8.7 16h-.8a.5.5 0 01-.5-.6l2.7-9c.1-.3.3-.4.5-.4h2.8c.2 0 .4.1.5.4l2.7 9a.5.5 0 01-.5.6h-.8a.5.5 0 01-.4-.4l-.7-2.2c0-.3-.3-.4-.5-.4h-3.4c-.2 0-.4.1-.5.4l-.7 2.2c0 .3-.2.4-.4.4zm2.6-7.6l-.6 2a.5.5 0 00.5.6h1.6a.5.5 0 00.5-.6l-.6-2c0-.3-.3-.4-.5-.4h-.4c-.2 0-.4.1-.5.4z"></path></g></svg>`

});
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: `<svg width="18" height="24" viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg"><g stroke="none" stroke-width="1" fill="currentColor" fill-rule="evenodd"><g ><g fill="currentColor"><g transform="translate(119.502295, 137.878331) rotate(-135.000000) translate(-119.502295, -137.878331) translate(48.002295, 31.757731)" ><path d="M100.946943,60.8084699 L43.7469427,60.8084699 C37.2852111,60.8084699 32.0469427,66.0467383 32.0469427,72.5084699 L32.0469427,118.70847 C32.0469427,125.170201 37.2852111,130.40847 43.7469427,130.40847 L100.946943,130.40847 C107.408674,130.40847 112.646943,125.170201 112.646943,118.70847 L112.646943,72.5084699 C112.646943,66.0467383 107.408674,60.8084699 100.946943,60.8084699 Z M93.646,79.808 L93.646,111.408 L51.046,111.408 L51.046,79.808 L93.646,79.808 Z" fill-rule="nonzero"></path><path d="M87.9366521,16.90916 L87.9194966,68.2000001 C87.9183543,69.4147389 86.9334998,70.399264 85.7187607,70.4 L56.9423078,70.4 C55.7272813,70.4 54.7423078,69.4150264 54.7423078,68.2 L54.7423078,39.4621057 C54.7423078,37.2523513 55.5736632,35.1234748 57.0711706,33.4985176 L76.4832996,12.4342613 C78.9534987,9.75382857 83.1289108,9.5834005 85.8093436,12.0535996 C87.1658473,13.303709 87.9372691,15.0644715 87.9366521,16.90916 Z" fill-rule="evenodd"></path><path d="M131.3,111.241199 L11.7,111.241199 C5.23826843,111.241199 0,116.479467 0,122.941199 L0,200.541199 C0,207.002931 5.23826843,212.241199 11.7,212.241199 L131.3,212.241199 C137.761732,212.241199 143,207.002931 143,200.541199 L143,122.941199 C143,116.479467 137.761732,111.241199 131.3,111.241199 Z M124,130.241 L124,193.241 L19,193.241 L19,130.241 L124,130.241 Z" fill-rule="nonzero"></path></g></g><path d="M51,218 L205,218 C211.075132,218 216,222.924868 216,229 C216,235.075132 211.075132,240 205,240 L51,240 C44.9248678,240 40,235.075132 40,229 C40,222.924868 44.9248678,218 51,218 Z" id="change-background-color-icon" style="fill:#FA541C"></path></g></g></svg>`

});
Expand All @@ -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();
},
Expand All @@ -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();
},
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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");
Expand All @@ -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() 方法读取剪贴板中的文本
Expand All @@ -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(""); // 清空选定的文本
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 1024ae8

Please sign in to comment.