Skip to content

Commit

Permalink
feat!(common): add codeblock editor in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Falcion committed Oct 22, 2024
1 parent 9e0f2df commit d2bc94a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions source/components/modals/codeblock-edit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Modal, Notice } from "obsidian";
import { ContextEditor } from "../contexts/contextEditor";
import { ContextEditCodeblocks } from "../contextEditCodeblock";
import UNITADE_PLUGIN from "../../main";

export class FenceEditModal extends Modal {
private codeEditor!: ContextEditor;

private constructor(
private plugin: UNITADE_PLUGIN,
private code: string,
private language: string,
private onSave: (changedCode: string) => void
) {
super(plugin.app);
}

onOpen() {
super.onOpen();

this.codeEditor = new ContextEditor(
this.contentEl,
this.plugin,
this.code,
this.language,
);

this.modalEl.setCssProps({
"--dialog-width": "90vw",
"--dialog-height": "90vh",
});

this.modalEl.style.height = "var(--dialog-height)";

const closeButton = this.modalEl.querySelector<HTMLDivElement>(".modal-close-button");

closeButton!.style.background = "var(--modal-background)";
closeButton!.style.zIndex = `${Number.MAX_SAFE_INTEGER}`;
}

onClose() {
super.onClose();

this.onSave(this.codeEditor.getValue());
}

static openOnCurrentCode(plugin: UNITADE_PLUGIN) {
const context = ContextEditCodeblocks.create(plugin);

if (!context.isInFence()) {
if(plugin.settings.silence_errors)
console.debug('SILENCED ERROR: Notice("Not valid codeblock");');
else
new Notice("Not valid codeblock");

return;
}

const fenceData = context.getFenceData();

if (!fenceData) return;
else
new FenceEditModal(
plugin,
fenceData.content,
fenceData.language,
(value) => context.replaceFenceContent(value)
).open();
}
}

0 comments on commit d2bc94a

Please sign in to comment.