Skip to content

Commit

Permalink
cleanup code, remove fundingUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
dzruyk committed Oct 22, 2024
1 parent 20acd62 commit cd13f12
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 127 deletions.
3 changes: 1 addition & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"id": "asciidoctor-editor",
"name": "Asciidoctor editor",
"version": "0.1.1",
"version": "0.1.2",
"minAppVersion": "0.15.0",
"description": "View and modify asciidoc pages",
"author": "dzruyk",
"authorUrl": "https://github.com/dzruyk",
"fundingUrl": "https://тыл-22.рф/",
"isDesktopOnly": true
}
15 changes: 6 additions & 9 deletions src/asciidocView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { basicExtensions } from "./codemirror";
import { SearchCtx } from "./searchCtx";
import { asciidoc } from "codemirror-asciidoc";
import { KeyInfo, KeyboardCallbacks } from "./keyboardCallbacks";
import { createEl, patchAdmonitionBlock } from "./util"
import { patchAdmonitionBlock } from "./util"

export const ASCIIDOC_EDITOR_VIEW = "asciidoc-editor-view";

declare var CodeMirror: any;
declare var Prism: any;
declare const CodeMirror: any;
declare const Prism: any;

function adoc() {return asciidoc; }
CodeMirror.defineMode("asciidoc", adoc)
Expand Down Expand Up @@ -227,9 +227,9 @@ export class AsciidocView extends TextFileView {

const parser = new window.DOMParser();

const dataEl = createEl("div", {class: "markdown-preview-view markdown-rendered node-insert-event allow-fold-headings show-indentation-guide allow-fold-lists show-properties adoc-preview" })
let root = document.createElement("div");
const dataEl = root.createEl("div", { cls : "markdown-preview-view markdown-rendered node-insert-event allow-fold-headings show-indentation-guide allow-fold-lists show-properties adoc-preview" });

//dataEl.innerHTML = htmlStr;
const parsedDoc = parser.parseFromString(htmlStr, "text/html")
if (parsedDoc.body && parsedDoc.body.childNodes.length > 0) {
let chldArr = parsedDoc.body.childNodes
Expand Down Expand Up @@ -319,7 +319,7 @@ export class AsciidocView extends TextFileView {
path = unescape(path);
let file = this.app.vault.getAbstractFileByPath(path);

if (file) {
if (file instanceof TFile) {
item.src = this.app.vault.getResourcePath(<TFile>file);
}
}
Expand All @@ -333,9 +333,6 @@ export class AsciidocView extends TextFileView {
console.log(err);
}

let root = document.createElement("div");
root.appendChild(dataEl)

this.sctx = new SearchCtx(dataEl, root);
parentEl.replaceChildren(root);
}
Expand Down
99 changes: 21 additions & 78 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,38 @@ import { App, Plugin, TFile, PluginSettingTab, Setting, Menu, MenuItem } from 'o
import { AdocNewFileModal } from './adocNewFileModal';
import { AsciidocView, ASCIIDOC_EDITOR_VIEW } from './asciidocView';

interface AsciidocPluginSettings {
mySetting: string;
}

const DEFAULT_SETTINGS: AsciidocPluginSettings = {
mySetting: 'default'
}

export default class AsciidocPlugin extends Plugin {
settings: AsciidocPluginSettings;
app: any;

async onload() {
await this.loadSettings();

this.registerExtensions(["adoc", "asciidoc"], ASCIIDOC_EDITOR_VIEW);
this.registerView(ASCIIDOC_EDITOR_VIEW, (leaf) => new AsciidocView(this, leaf))

// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');

this.registerEvent(
this.app.workspace.on("file-menu", (menu: Menu, file: TFile) => {
menu.addItem((item: MenuItem) => {
item
.setTitle("New asciidoc file")
.setIcon("scroll-text")
.onClick(async () => {
new AdocNewFileModal(this, file).open();
this.registerEvent(
this.app.workspace.on("file-menu", (menu: Menu, file: TFile) => {
menu.addItem((item: MenuItem) => {
item
.setTitle("New asciidoc file")
.setIcon("scroll-text")
.onClick(async () => {
new AdocNewFileModal(this, file).open();
});
});
});
})
);
})
);

this.addRibbonIcon('scroll-text', "New asciidoc file", () => {
new AdocNewFileModal(this).open();
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'create-adoc',
name: 'create new Asciidoc file',
callback: () => {
new AdocNewFileModal(this).open();
}
});
this.addRibbonIcon('scroll-text', "New asciidoc file", () => {
new AdocNewFileModal(this).open();
});
this.addCommand({
id: 'create-adoc',
name: 'create new Asciidoc file',
callback: () => {
new AdocNewFileModal(this).open();
}
});
}

public updateEditorExtensions() {
this.app.workspace.updateOptions();
}

onunload() {
this.app.viewRegistry.unregisterExtensions([".adoc", ".asciidoc"]);
//this.app.workspace.detachLeavesOfType(ASCIIDOC_EDITOR_VIEW);
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}

class AsciidocSettingTab extends PluginSettingTab {
plugin: AsciidocPlugin;

constructor(app: App, plugin: AsciidocPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
const {containerEl} = this;

containerEl.empty();

new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
}
}

29 changes: 11 additions & 18 deletions src/searchCtx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Mark from 'mark.js'
import { createEl } from "./util"

export class SearchCtx {
private isSearchActive: boolean;
Expand All @@ -21,24 +20,18 @@ export class SearchCtx {
render() {
this.isSearchActive = true;

let searchBox = createEl("div", { class: "CodeMirror-dialog CodeMirror-dialog-top" })
searchBox.appendChild(
createEl("span", {
class: "CodeMirror-search-label",
textContent: "Search:",
})
)
searchBox.appendChild(
createEl("input", {
let searchBox = createEl("div", { cls: "CodeMirror-dialog CodeMirror-dialog-top" })

searchBox.createEl("span", {
cls: "CodeMirror-search-label",
text: "Search:",
})
searchBox.createEl("input", {
type: "text",
class: "CodeMirror-search-field"
})
)
searchBox.appendChild(
createEl("span", {
class: "CodeMirror-search-hint",
})
)
cls: "CodeMirror-search-field"
})

searchBox.createEl("span", { cls: "CodeMirror-search-hint" })

this.searchContainer.insertBefore(searchBox, this.searchContainer.children[0]);
let collection = searchBox.getElementsByTagName("input");
Expand Down
23 changes: 3 additions & 20 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import { setIcon } from 'obsidian';

export function createEl(tagName: string, attrs?: any): HTMLElement {
const c = document.createElement(tagName)
if (!attrs)
return c
for (let p in attrs) {
if (!p || !Object.prototype.hasOwnProperty.call(attrs, p))
continue
if (p == "textContent") {
c.setText(attrs[p])
} else {
c.setAttribute(p, attrs[p])
}
}
return c
}

const adocSpecificTypes = ["important", "caution"];
const admotionIcons: Map<string, string> = new Map([
["danger", "zap"],
Expand All @@ -34,14 +18,13 @@ export function patchAdmonitionBlock(item: HTMLElement)
item.setAttribute("data-callout", abType);
item.className = "callout";

let calloutTitle = createEl("div", { class: "callout-title" });
const calloutIcon = createEl("div", { class: "callout-icon" });
let calloutTitle = createEl("div", { cls: "callout-title" });
let iconName = admotionIcons.get(abType);
if (iconName == undefined)
iconName = "pencil";
setIcon(calloutIcon, iconName);

calloutTitle.appendChild(calloutIcon)
const calloutIcon = calloutTitle.createEl("div", {cls: "callout-icon" });
setIcon(calloutIcon, iconName);
if (iconElement.length > 0 && iconElement[0]) {
calloutTitle.appendChild(iconElement[0])
item.insertBefore(calloutTitle, item.firstChild);
Expand Down

0 comments on commit cd13f12

Please sign in to comment.