Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
fix: exchangeCenterTitleBar
Browse files Browse the repository at this point in the history
  • Loading branch information
kometenstaub committed Jul 4, 2022
1 parent d952a6c commit ab98d70
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
16 changes: 11 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getRightTitleBar,
getTitlebarText,
removeAllPageHeaderButtons,
removeAllTitleBarButtons,
removeAllTitleBarButtons, removeTitlebarText,
restoreCenterTitlebar,
} from './utils';
import { lucideIcons } from './lucide';
Expand All @@ -40,7 +40,7 @@ export default class TopBarButtonsPlugin extends Plugin {
settings!: TopBarButtonsSettings;
iconList: string[] = obsiIcons.concat(lucideIcons);
listener!: () => void;
titlebarText: string[] = [];
titlebarText: Element[] = [];
windows: Document[] = [];

addPageHeaderButton(
Expand Down Expand Up @@ -152,6 +152,7 @@ export default class TopBarButtonsPlugin extends Plugin {
// before any button is added, the parent needs to be removed and the
// own parent added; this requires checks in the settings modal when
// buttons are added and removed
/*
addInitialCenterTitleBarButtons(doc: Document) {
if (this.settings.titleCenter.length > 0) {
const center = exchangeCenterTitleBar(doc);
Expand All @@ -163,6 +164,7 @@ export default class TopBarButtonsPlugin extends Plugin {
}
}
}
*/

addCenterTitleBarButtons(doc: Document) {
if (this.settings.titleCenter.length > 0) {
Expand All @@ -179,9 +181,13 @@ export default class TopBarButtonsPlugin extends Plugin {
initTitleBar(doc: Document) {
this.addLeftTitleBarButtons(doc);
this.addRightTitleBarButtons(doc);
this.titlebarText.push(getTitlebarText(doc));
// store the element so that it can be restored later on
if (this.settings.titleCenter.length > 0) {
this.addInitialCenterTitleBarButtons(doc);
this.titlebarText.push(getTitlebarText(doc));
removeTitlebarText(doc)
// could be passed; maybe for next refactoring
const newActions = exchangeCenterTitleBar(doc)
this.addCenterTitleBarButtons(doc);
}
}
async onload() {
Expand All @@ -192,8 +198,8 @@ export default class TopBarButtonsPlugin extends Plugin {

this.app.workspace.onLayoutReady(() => {
if (Platform.isDesktopApp) {
this.initTitleBar(document)
this.windows.push(document)
this.initTitleBar(document)
}
if (Platform.isMobile || this.settings.desktop) {
this.addButtonsToAllLeaves();
Expand Down
12 changes: 10 additions & 2 deletions src/ui/commandSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import type {
} from '../interfaces';
import type TopBarButtonsPlugin from '../main';
import {
exchangeCenterTitleBar,
getTitlebarText,
removeAllTitleBarButtons,
removeCenterTitleBarButtons,
removeLeftTitleBarButtons,
removeRightTitleBarButtons,
removeRightTitleBarButtons, removeTitlebarText,
restoreCenterTitlebar,
} from '../utils';

Expand Down Expand Up @@ -92,8 +94,14 @@ export default class CommandSuggester extends FuzzySuggestModal<Command> {
} else if (this.type === 'title-center') {
// initial button is added
if (centerCounter === 0) {
this.plugin.titlebarText = [];
for (let i = 0; i < this.plugin.windows.length; i++) {
this.plugin.addInitialCenterTitleBarButtons(this.plugin.windows[i])
const doc = this.plugin.windows[i]
this.plugin.titlebarText.push(getTitlebarText(doc));
removeTitlebarText(doc)
// could be passed; maybe for next refactoring
const newActions = exchangeCenterTitleBar(doc)
this.plugin.addCenterTitleBarButtons(doc)
}
// all buttons removed
} else if (this.plugin.settings.titleCenter.length === 0) {
Expand Down
12 changes: 10 additions & 2 deletions src/ui/iconPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
import type TopBarButtonsPlugin from '../main';
import type { Buttons, TitleOrPage } from 'src/interfaces';
import {
exchangeCenterTitleBar,
getTitlebarText,
removeCenterTitleBarButtons,
removeLeftTitleBarButtons,
removeRightTitleBarButtons,
removeRightTitleBarButtons, removeTitlebarText,
restoreCenterTitlebar,
} from '../utils';

Expand Down Expand Up @@ -138,8 +140,14 @@ export default class IconPicker extends FuzzySuggestModal<string> {
} else {
// initial button is added
if (centerCounter === 0) {
this.plugin.titlebarText = [];
for (let i = 0; i < this.plugin.windows.length; i++) {
this.plugin.addInitialCenterTitleBarButtons(this.plugin.windows[i])
const doc = this.plugin.windows[i]
this.plugin.titlebarText.push(getTitlebarText(doc));
removeTitlebarText(doc)
// could be passed; maybe for next refactoring
const newActions = exchangeCenterTitleBar(doc)
this.plugin.addCenterTitleBarButtons(doc)
}
} else if (settings.titleCenter.length === 0) {
for (let i = 0; i < this.plugin.windows.length; i++) {
Expand Down
38 changes: 17 additions & 21 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Platform, setIcon } from 'obsidian';
import {
PLUGIN_CLASS_NAME,
TITLEBAR_CENTER,
TITLEBAR_CLASS,
} from './constants';
import {Platform, setIcon} from 'obsidian';
import {PLUGIN_CLASS_NAME, TITLEBAR_CENTER, TITLEBAR_CLASS,} from './constants';

// General purpose utility functions

Expand Down Expand Up @@ -77,26 +73,25 @@ export function removeSingleButton(
// Center title bar utility functions

export function getTitlebarText(doc: Document) {
const titlebarText = document.getElementsByClassName('titlebar-text')[0];
return titlebarText.getText();
return doc.getElementsByClassName('titlebar-text')[0]
}

export function removeTitlebarText() {
const titlebarText = document.getElementsByClassName('titlebar-text');
removeElements(titlebarText);
export function removeTitlebarText(doc: Document) {
const titlebarText = doc.getElementsByClassName('titlebar-text')[0];
titlebarText.detach();
}

export function restoreCenterTitlebar(text: string, doc: Document) {
export function restoreCenterTitlebar(titlebarText: Element, doc: Document) {
const centerTitlebar = doc.getElementsByClassName(
`${PLUGIN_CLASS_NAME} ${TITLEBAR_CENTER}`
)[0];
// needed for ununload if no center buttons are defined
if (centerTitlebar !== undefined) {
centerTitlebar.classList.remove(
...[PLUGIN_CLASS_NAME, TITLEBAR_CENTER]
);
centerTitlebar.addClass('titlebar-text');
centerTitlebar.innerHTML = text;
const inner = centerTitlebar.parentElement
centerTitlebar.detach()
if (inner) {
inner.insertBefore(titlebarText, inner.children[0])
}
}
}

Expand All @@ -109,10 +104,11 @@ export function removeCenterTitleBarButtons(doc: Document) {
}

export function exchangeCenterTitleBar(doc: Document): Element {
const centerTitleBar = doc.getElementsByClassName('titlebar-text')[0];
centerTitleBar.classList.remove('titlebar-text');
centerTitleBar.addClasses([PLUGIN_CLASS_NAME, TITLEBAR_CENTER]);
centerTitleBar.innerHTML = '';
const centerTitleBar = createDiv({cls: [PLUGIN_CLASS_NAME, 'titlebar-center']})
const parent = doc.getElementsByClassName('titlebar-inner')[0]
if (parent) {
parent.insertBefore(centerTitleBar, parent.children[0])
}
return centerTitleBar;
}

Expand Down

0 comments on commit ab98d70

Please sign in to comment.