Skip to content

Commit

Permalink
Merge pull request #14 from OfficerHalf/linked-panes
Browse files Browse the repository at this point in the history
Linked panes
  • Loading branch information
nathonius authored Nov 10, 2021
2 parents 25ed7b7 + c5ae965 commit 9416579
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 30 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 1.3.0

## Features

- Support adding classes to linked panes.

## Fixes

- Remove a bunch of extra console logs that got left in.

# 1.2.1

## Fixes
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "auto-class",
"name": "Auto Class",
"version": "1.2.1",
"version": "1.3.0",
"minAppVersion": "0.12.12",
"description": "Automatically apply CSS classes to the markdown view based on a note's path.",
"author": "OfficerHalf",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-class",
"version": "1.2.1",
"version": "1.3.0",
"description": "Automatically apply CSS classes to the markdown view based on a note's path.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { AutoClassPluginSettings } from './interfaces';

export const DEFAULT_SETTINGS: AutoClassPluginSettings = {
paths: [{ path: 'Example Path/Subfolder/', classes: ['example-class'], scope: ClassPathScope.Preview }],
version: '1.2.1'
version: '1.3.0'
};
93 changes: 68 additions & 25 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AutoClassPluginSettingsTab } from './settings';
export class AutoClassPlugin extends Plugin {
appliedClasses: ViewAppliedClasses[] = [];
settings: AutoClassPluginSettings = DEFAULT_SETTINGS;

async onload() {
const savedData = await this.loadData();
this.settings = Object.assign({}, DEFAULT_SETTINGS, savedData);
Expand All @@ -21,59 +22,92 @@ export class AutoClassPlugin extends Plugin {
this.removeAllClasses();
}

/**
* Layout change event handler. Removes old classes
* and applies new classes to the active view and
* any linked panes.
*/
handleLayoutChange(): void {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
console.log('REMOVING PREVIOUS');
this.removePreviousClasses(activeView);
let matches: ClassPath[] = [];
let container: Element;
if (this.isPreivewMode(activeView)) {
console.log('IS PREVIEW MODE');
matches = this.settings.paths.filter(
(path) =>
(path.scope === ClassPathScope.Preview || path.scope === ClassPathScope.Both) &&
activeView.file.path.startsWith(path.path)
);
container = this.getPreviewContainer(activeView);
} else if (this.isEditMode(activeView)) {
console.log('IS EDIT MODE');
matches = this.settings.paths.filter(
(path) =>
(path.scope === ClassPathScope.Edit || path.scope === ClassPathScope.Both) &&
activeView.file.path.startsWith(path.path)
);
container = this.getEditContainer(activeView);
// Get any linked views
let activeViews: MarkdownView[] = [activeView];
const leafGroup = this.app.workspace.getGroupLeaves((activeView.leaf as any).group);
if (leafGroup) {
activeViews = leafGroup
.map((leaf) => leaf.view)
.filter((view) => view instanceof MarkdownView) as MarkdownView[];
}
console.log(matches);
console.log(container);
const classes: string[] = matches.flatMap((match) => match.classes);
console.log(classes);
this.applyClasses(classes, activeView, container);

// Remove and apply classes for each applicable view
activeViews.forEach((view) => {
this.removePreviousClasses(view);
let matches: ClassPath[] = [];
let container: Element;
if (this.isPreivewMode(view)) {
matches = this.settings.paths.filter(
(path) =>
(path.scope === ClassPathScope.Preview || path.scope === ClassPathScope.Both) &&
view.file.path.startsWith(path.path)
);
container = this.getPreviewContainer(view);
} else if (this.isEditMode(view)) {
matches = this.settings.paths.filter(
(path) =>
(path.scope === ClassPathScope.Edit || path.scope === ClassPathScope.Both) &&
view.file.path.startsWith(path.path)
);
container = this.getEditContainer(view);
}
const classes: string[] = matches.flatMap((match) => match.classes);
this.applyClasses(classes, view, container);
});
}
}

/**
* Save settings in plugin data
*/
saveSettings(): Promise<void> {
return this.saveData(this.settings);
}

/**
* Given a string of comma separated classnames,
* return them as an array
*/
getClassList(classString: string): string[] {
return classString.split(',').map((cls) => cls.trim());
}

/**
* Returns true if a view is in preview mode
*/
private isPreivewMode(view: MarkdownView): boolean {
return (view.currentMode as any).type === 'preview';
}

/**
* Returns true if a view is in edit/source mode
*/
private isEditMode(view: MarkdownView): boolean {
return (view.currentMode as any).type === 'source';
}

/**
* Add classes to an html element and store the
* applied classes along with a reference the view
* they were added to for removal later.
*/
private applyClasses(classes: string[], view: MarkdownView, container: Element): void {
container.addClasses(classes);
this.appliedClasses.push({ view, classes });
}

/**
* Given a view, remove any extra classes this plugin
* has applied to that view.
*/
private removePreviousClasses(view: MarkdownView): void {
const previewContainer = this.getPreviewContainer(view);
const editContainer = this.getEditContainer(view);
Expand All @@ -93,6 +127,9 @@ export class AutoClassPlugin extends Plugin {
this.appliedClasses = newApplied;
}

/**
* Remove all applied classes from all views
*/
private removeAllClasses() {
this.appliedClasses.forEach((applied) => {
if (applied.view) {
Expand All @@ -108,10 +145,16 @@ export class AutoClassPlugin extends Plugin {
});
}

/**
* Get the element that preview classes are applied to
*/
private getPreviewContainer(view: MarkdownView): Element {
return view.contentEl.querySelector('.markdown-preview-view');
}

/**
* Get the element that edit/source classes are applied to
*/
private getEditContainer(view: MarkdownView): Element {
return view.contentEl.querySelector('.markdown-source-view');
}
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.3.0": "0.12.12",
"1.2.1": "0.12.12",
"1.2.0": "0.12.12",
"1.1.0": "0.12.12",
Expand Down

0 comments on commit 9416579

Please sign in to comment.