Skip to content

Commit

Permalink
fix: Click on Admonition codeblocks to edit in LP (close #278)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Sep 14, 2023
1 parent 9143f3f commit 4d993bb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
addIcon,
Component,
editorLivePreviewField,
livePreviewState,
MarkdownPostProcessor,
MarkdownPostProcessorContext,
MarkdownPreviewRenderer,
Expand Down Expand Up @@ -71,6 +73,14 @@ declare module "obsidian" {
namespace MarkdownPreviewRenderer {
function unregisterCodeBlockPostProcessor(lang: string): void;
}
interface Editor {
cm: {
state: EditorState;
focus: () => void;
posAtDOM: (el: HTMLElement) => number;
dispatch: (tr: TransactionSpec) => void;
};
}
}

import AdmonitionSetting from "./settings";
Expand All @@ -79,6 +89,7 @@ import { InsertAdmonitionModal } from "./modal";
import { IconName } from "@fortawesome/fontawesome-svg-core";
import CalloutManager from "./callout/manager";
import { AdmonitionSuggest } from "./suggest/suggest";
import { EditorState, TransactionSpec } from "@codemirror/state";

const DEFAULT_APP_SETTINGS: AdmonitionSettings = {
userAdmonitions: {},
Expand Down Expand Up @@ -354,6 +365,28 @@ ${editor.getDoc().getSelection()}
);
}
el.replaceWith(admonitionElement);

const view = app.workspace.getActiveViewOfType(MarkdownView);
//hack
//@ts-ignore
if (view?.editor?.cm?.state?.field(editorLivePreviewField)) {
const editor = view.editor.cm;
admonitionElement.onClickEvent((ev) => {
if (ev.defaultPrevented || ev.detail > 1 || ev.shiftKey)
return;
try {
const pos = editor.posAtDOM(admonitionElement);
editor.focus();
editor.dispatch({
selection: {
head: pos,
anchor: pos
}
});
} catch (e) {}
});
}

return admonitionElement;
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 4d993bb

Please sign in to comment.