This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
WeakMap
for state handling and check md rendering context.
- Loading branch information
1 parent
ca4e6a9
commit a748bc5
Showing
5 changed files
with
63 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,38 @@ | ||
import { Plugin } from 'obsidian'; | ||
import { onHeadClick, TTableStates, AttributeName, resetTable } from 'src/sortable'; | ||
|
||
interface SortableSettings { | ||
mySetting: string; | ||
} | ||
|
||
const DEFAULT_SETTINGS: SortableSettings = { | ||
mySetting: 'default' | ||
}; | ||
|
||
export default class SortablePlugin extends Plugin { | ||
settings: SortableSettings; | ||
tableStates: TTableStates; | ||
|
||
async onload(): Promise<void> { | ||
console.log('Sortable: loading plugin...'); | ||
|
||
await this.loadSettings(); | ||
|
||
this.tableStates = {}; | ||
|
||
this.registerDomEvent(document, 'click', (ev: MouseEvent) => onHeadClick(ev, this.tableStates)); | ||
|
||
console.log('Sortable: loaded plugin.'); | ||
} | ||
|
||
onunload(): void { | ||
// iterate through all table elements in the document | ||
// and remove the table attribute | ||
const tables = Array.from(document.querySelectorAll('table')); | ||
for (const table of tables) { | ||
const tableID: string | null = table.getAttribute(AttributeName.table); | ||
|
||
// this table is in the default state | ||
if (tableID === null) { | ||
continue; | ||
} | ||
|
||
const state = this.tableStates[tableID]; | ||
|
||
// restore original order | ||
resetTable(state, table.querySelector('tbody')); | ||
|
||
// remove "sortable" attribute | ||
table.removeAttribute(AttributeName.table); | ||
|
||
// remove tableHeader attribute | ||
const th = table.querySelector(`thead th:nth-child(${state.columnIdx + 1})`); | ||
th.removeAttribute(AttributeName.tableHeader); | ||
|
||
delete this.tableStates[tableID]; | ||
} | ||
|
||
// delete remaining keys in the tableStates object | ||
// ideally, this should not be necessary, see (#16) | ||
const remKeys = Object.keys(this.tableStates); | ||
for (const key of remKeys) { | ||
delete this.tableStates[key]; | ||
} | ||
|
||
console.log('Sortable: unloaded plugin.'); | ||
} | ||
|
||
async loadSettings(): Promise<void> { | ||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | ||
} | ||
|
||
async saveSettings(): Promise<void> { | ||
await this.saveData(this.settings); | ||
} | ||
} | ||
import { Plugin } from "obsidian"; | ||
import { onHeadClick, TTableStates, AttributeName, resetTable } from "src/sortable"; | ||
|
||
export default class SortablePlugin extends Plugin { | ||
tableStates: TTableStates; | ||
|
||
async onload(): Promise<void> { | ||
console.log("Sortable: loading plugin..."); | ||
|
||
this.tableStates = new WeakMap(); | ||
|
||
this.registerDomEvent(document, "click", (ev: MouseEvent) => onHeadClick(ev, this.tableStates)); | ||
|
||
console.log("Sortable: loaded plugin."); | ||
} | ||
|
||
onunload(): void { | ||
// get all HTMLTableElements present in the map and reset their state | ||
const tables = Array.from(document.querySelectorAll("table")); | ||
|
||
for (const table of tables) { | ||
if (this.tableStates.has(table)) { | ||
const state = this.tableStates.get(table); | ||
|
||
resetTable(state, table.querySelector("tbody")); | ||
|
||
const th = table.querySelector(`thead th:nth-child(${state.columnIdx + 1})`); | ||
th.removeAttribute(AttributeName.tableHeader); | ||
|
||
this.tableStates.delete(table); | ||
} | ||
} | ||
|
||
delete this.tableStates; | ||
|
||
console.log("Sortable: unloaded plugin."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters