diff --git a/main.js b/main.js index 74ba0ec..27536e2 100644 --- a/main.js +++ b/main.js @@ -6762,6 +6762,7 @@ var eventTypes = { updateSections: "mkmd-update-sections", settingsChanged: "mkmd-settings-changed", spawnPortal: "mkmd-portal-spawn", + loadPortal: "mkmd-portal-load", openFilePortal: "mkmd-portal-file", focusPortal: "mkmd-portal-focus" }; @@ -8157,6 +8158,12 @@ var createFlowEditorInElement = (id, el, type, file, from, to) => { }); activeWindow.dispatchEvent(evt); }; +var loadFlowEditorByDOM = (el, view, id) => { + let evt = new CustomEvent(eventTypes.loadPortal, { + detail: { id, el, view } + }); + activeWindow.dispatchEvent(evt); +}; var focusFlowEditor = (id, top2) => { let evt = new CustomEvent(eventTypes.focusPortal, { detail: { id, parent: false, top: top2 } @@ -8798,12 +8805,12 @@ var parseOutReferences = (ostr) => { var getFileFromString = (url, source) => { return app.metadataCache.getFirstLinkpathDest(url, source); }; -var getLineRangeFromRef = (file, ref, app2) => { +var getLineRangeFromRef = (file, ref, plugin) => { var _a2; if (!ref) { return [void 0, void 0]; } - const cache = app2.metadataCache.getFileCache(file); + const cache = app.metadataCache.getFileCache(file); const headings = cache.headings; const blocks = cache.blocks; const sections2 = cache.sections; @@ -8818,7 +8825,7 @@ var getLineRangeFromRef = (file, ref, app2) => { const index = headings.findIndex((f4) => f4.heading == heading.heading); const level = (_a2 = headings[index]) == null ? void 0 : _a2.level; const nextIndex2 = headings.findIndex((f4, i3) => i3 > index && f4.level <= level); - const start2 = window.make.settings.editorFlowStyle == "classic" ? heading.position.start.line + 1 : heading.position.start.line + 2; + const start2 = plugin.settings.editorFlowStyle == "classic" ? heading.position.start.line + 1 : heading.position.start.line + 2; if (index < headings.length - 1 && nextIndex2 != -1) { return [start2, headings[nextIndex2].position.end.line]; } @@ -8826,7 +8833,7 @@ var getLineRangeFromRef = (file, ref, app2) => { } return [void 0, void 0]; }; -var loadFlowEditorByDOM = (el, view, id) => { +var loadFlowEditorByDOM2 = (make, el, view, id) => { setTimeout(async () => { let counter = 0; while (!el.parentElement && counter++ <= 50) @@ -8850,22 +8857,22 @@ var loadFlowEditorByDOM = (el, view, id) => { var _a2; const cm = (_a2 = leaf.view.editor) == null ? void 0 : _a2.cm; if (cm && view.dom == cm.dom) { - loadFlowEditorsForLeafForID(cm, leaf, app, id); + loadFlowEditorsForLeafForID(cm, leaf, make, id); } }, app.workspace["rootSplit"]); }); }); }; -var loadFlowEditorsForLeafForID = (cm, leaf, app2, id) => { +var loadFlowEditorsForLeafForID = (cm, leaf, make, id) => { const stateField = cm.state.field(flowEditorInfo, false); if (!stateField) return; const flowInfo = stateField.find((f4) => f4.id == id); if (flowInfo && flowInfo.expandedState == 2) { - loadFlowEditor(cm, flowInfo, leaf, app2); + loadFlowEditor(cm, flowInfo, leaf, make); } }; -var loadFlowEditor = (cm, flowEditorInfo2, leaf, app2) => { +var loadFlowEditor = (cm, flowEditorInfo2, leaf, make) => { var _a2; const dom = cm.dom.querySelector("#mk-flow-" + flowEditorInfo2.id); const [link, ref] = parseOutReferences(flowEditorInfo2.link); @@ -8873,7 +8880,7 @@ var loadFlowEditor = (cm, flowEditorInfo2, leaf, app2) => { const file = getFileFromString(link, source); if (dom) { if (file) { - const selectiveRange = getLineRangeFromRef(file, ref, app2); + const selectiveRange = getLineRangeFromRef(file, ref, make); if (!dom.hasAttribute("ready")) { dom.setAttribute("ready", ""); createFlowEditorInElement(flowEditorInfo2.id, dom, ref ? "block" : "flow", file.path, selectiveRange[0], selectiveRange[1]); @@ -8885,8 +8892,8 @@ var loadFlowEditor = (cm, flowEditorInfo2, leaf, app2) => { const createFile = async (e3) => { e3.stopPropagation(); e3.stopImmediatePropagation(); - await app2.fileManager.createNewMarkdownFile(app2.vault.getRoot(), link); - loadFlowEditor(cm, flowEditorInfo2, leaf, app2); + await app.fileManager.createNewMarkdownFile(app.vault.getRoot(), link); + loadFlowEditor(cm, flowEditorInfo2, leaf, make); }; createDiv.setText(`"${link}" ` + i18n_default.labels.noFile); createDiv.addEventListener("click", createFile); @@ -23545,11 +23552,12 @@ var Mark = (props) => { // src/cm-extensions/inlineStylerView/InlineMenu.tsx var import_classnames3 = __toESM(require_classnames()); -var loadStylerIntoContainer = (el) => { +var loadStylerIntoContainer = (el, plugin) => { const root = createRoot(el); root.render(/* @__PURE__ */ bn.createElement(InlineMenuComponent, { mobile: true, - activeMarks: [] + activeMarks: [], + plugin })); }; var InlineMenuComponent = (props) => { @@ -23573,7 +23581,7 @@ var InlineMenuComponent = (props) => { if (!cm) return; const end2 = cm.state.selection.main.to; - const insertChars = cm.state.sliceDoc(end2 - 1, end2) == cm.state.lineBreak ? window.make.settings.menuTriggerChar : cm.state.lineBreak + window.make.settings.menuTriggerChar; + const insertChars = cm.state.sliceDoc(end2 - 1, end2) == cm.state.lineBreak ? props.plugin.settings.menuTriggerChar : cm.state.lineBreak + props.plugin.settings.menuTriggerChar; cm.dispatch({ changes: { from: end2, @@ -23633,7 +23641,7 @@ var InlineMenuComponent = (props) => { "aria-label": !platformIsMobile() ? i18n_default.commands.image : void 0, onMouseDown: () => { const view = getActiveMarkdownView(); - window.make.app.commands.commands["editor:attach-file"].editorCallback(view.editor, view); + props.plugin.app.commands.commands["editor:attach-file"].editorCallback(view.editor, view); }, className: "mk-mark", dangerouslySetInnerHTML: { __html: markIconSet["mk-make-attach"] } @@ -23641,7 +23649,7 @@ var InlineMenuComponent = (props) => { "aria-label": !platformIsMobile() ? i18n_default.commands.toggleKeyboard : void 0, onMouseDown: () => { const view = getActiveMarkdownView(); - window.make.app.commands.commands["editor:toggle-keyboard"].editorCallback(view.editor, view); + props.plugin.app.commands.commands["editor:toggle-keyboard"].editorCallback(view.editor, view); }, className: "mk-mark", dangerouslySetInnerHTML: { __html: markIconSet["mk-make-keyboard"] } @@ -23688,7 +23696,7 @@ var InlineMenuComponent = (props) => { active: props.activeMarks.find((f4) => f4 == s3.mark) ? true : false, toggleMarkAction }); - }), window.make.settings.inlineStylerColors ? /* @__PURE__ */ bn.createElement(bn.Fragment, null, /* @__PURE__ */ bn.createElement("div", { + }), props.plugin.settings.inlineStylerColors ? /* @__PURE__ */ bn.createElement(bn.Fragment, null, /* @__PURE__ */ bn.createElement("div", { className: "mk-divider" }), /* @__PURE__ */ bn.createElement("div", { "aria-label": !platformIsMobile() ? i18n_default.styles.textColor : void 0, @@ -23724,16 +23732,16 @@ var InlineMenuComponent = (props) => { }; // src/cm-extensions/inlineStylerView/inlineStyler.tsx -var cursorTooltipField = import_state10.StateField.define({ - create: getCursorTooltips, +var cursorTooltipField = (plugin) => import_state10.StateField.define({ + create: getCursorTooltips(plugin), update(tooltips3, tr) { if (!tr.docChanged && !tr.selection) return tooltips3; - return getCursorTooltips(tr.state); + return getCursorTooltips(plugin)(tr.state); }, provide: (f4) => showTooltip.computeN([f4], (state) => state.field(f4)) }); -function getCursorTooltips(state) { +var getCursorTooltips = (plugin) => (state) => { return state.selection.ranges.filter((range) => !range.empty).map((range) => { const expandedRange = expandRange(range, state); let line = state.doc.lineAt(range.head); @@ -23748,6 +23756,7 @@ function getCursorTooltips(state) { dom.className = "cm-tooltip-cursor"; const reactElement = createRoot(dom); reactElement.render(/* @__PURE__ */ bn.createElement(bn.Fragment, null, /* @__PURE__ */ bn.createElement(InlineMenuComponent, { + plugin, cm: view, activeMarks, mobile: false @@ -23756,9 +23765,9 @@ function getCursorTooltips(state) { } }; }); -} -function cursorTooltip() { - return cursorTooltipField; +}; +function cursorTooltip(plugin) { + return cursorTooltipField(plugin); } // src/cm-extensions/flowEditor/flowViewUpdates.ts @@ -23829,7 +23838,7 @@ var placeholder = import_state11.StateField.define({ var cmExtensions = (plugin, mobile) => { let extensions = [toggleMarkExtension, tooltips({ parent: document.body })]; if (!mobile && plugin.settings.inlineStyler) { - extensions.push(cursorTooltip()); + extensions.push(cursorTooltip(plugin)); } if (plugin.settings.markSans) { if (!mobile) { @@ -24167,6 +24176,7 @@ var MakeMDPlugin = class extends import_obsidian15.Plugin { replaceAllEmbed(element, context); }); window.addEventListener(eventTypes.spawnPortal, this.spawnPortal); + window.addEventListener(eventTypes.loadPortal, this.loadPortal); window.addEventListener(eventTypes.focusPortal, this.focusPortal); window.addEventListener(eventTypes.openFilePortal, this.openFileFromPortal); } @@ -24198,10 +24208,9 @@ var MakeMDPlugin = class extends import_obsidian15.Plugin { this.registerEditorSuggest(new MakeMenu(this.app, this)); this.registerEditorSuggest(new StickerMenu(this.app, this)); if (platformIsMobile() && this.settings.mobileMakeBar) - loadStylerIntoContainer(app.mobileToolbar.containerEl); + loadStylerIntoContainer(app.mobileToolbar.containerEl, this); } async onload() { - window.make = this; (0, import_obsidian15.addIcon)("mk-logo", mkLogo); console.log("Loading Make.md"); this.addSettingTab(new MakeMDPluginSettingsTab(this.app, this)); @@ -24214,6 +24223,9 @@ var MakeMDPlugin = class extends import_obsidian15.Plugin { openFileFromPortal(e3) { openFileFromPortal(this, e3); } + loadPortal(e3) { + loadFlowEditorByDOM2(this, e3.detail.el, e3.detail.view, e3.detail.id); + } spawnPortal(e3) { spawnNewPortal(this, e3); } @@ -24233,6 +24245,7 @@ var MakeMDPlugin = class extends import_obsidian15.Plugin { onunload() { console.log("Unloading Make.md"); window.removeEventListener(eventTypes.spawnPortal, this.spawnPortal); + window.removeEventListener(eventTypes.loadPortal, this.loadPortal); window.removeEventListener(eventTypes.focusPortal, this.focusPortal); window.removeEventListener(eventTypes.openFilePortal, this.openFileFromPortal); this.detachFileTreeLeafs(); diff --git a/src/cm-extensions/cmExtensions.ts b/src/cm-extensions/cmExtensions.ts index f5a394e..dbdce5b 100644 --- a/src/cm-extensions/cmExtensions.ts +++ b/src/cm-extensions/cmExtensions.ts @@ -19,7 +19,7 @@ import { tooltips } from "./tooltip"; export const cmExtensions = (plugin: MakeMDPlugin, mobile: boolean) => { let extensions = [toggleMarkExtension, tooltips({ parent: document.body })]; if (!mobile && plugin.settings.inlineStyler) { - extensions.push(cursorTooltip()); + extensions.push(cursorTooltip(plugin)); } if (plugin.settings.markSans) { if (!mobile) { diff --git a/src/cm-extensions/flowEditor/flowEditor.tsx b/src/cm-extensions/flowEditor/flowEditor.tsx index 296061b..f358cce 100644 --- a/src/cm-extensions/flowEditor/flowEditor.tsx +++ b/src/cm-extensions/flowEditor/flowEditor.tsx @@ -25,10 +25,10 @@ import { Tooltip, hoverTooltip } from "cm-extensions/tooltip"; import React from "react"; import ReactDOM from "react-dom"; import { createRoot } from "react-dom/client"; -import { loadFlowEditorByDOM } from "utils/flowEditor"; + import { info } from "console"; import { openFile } from "utils/utils"; -import { openFileFlowEditor } from "dispatch/flowDispatch"; +import { loadFlowEditorByDOM, openFileFlowEditor } from "dispatch/flowDispatch"; import { FlowEditorHover } from "components/FlowEditor/FlowEditorHover"; import { MarkdownView } from "obsidian"; import t from "i18n"; diff --git a/src/cm-extensions/inlineStylerView/InlineMenu.tsx b/src/cm-extensions/inlineStylerView/InlineMenu.tsx index 2333088..a08cae9 100644 --- a/src/cm-extensions/inlineStylerView/InlineMenu.tsx +++ b/src/cm-extensions/inlineStylerView/InlineMenu.tsx @@ -15,11 +15,11 @@ import { markIconSet, uiIconSet } from "utils/icons"; import MakeMenu from "components/MakeMenu/MakeMenu"; import classNames from "classnames"; -export const loadStylerIntoContainer = (el: HTMLElement) => { +export const loadStylerIntoContainer = (el: HTMLElement, plugin: MakeMDPlugin) => { // el.removeChild(el.querySelector('.mobile-toolbar-options-container')) const root = createRoot(el); root.render( - + ); }; @@ -27,6 +27,7 @@ export const InlineMenuComponent: React.FC<{ cm?: EditorView; activeMarks: string[]; mobile: boolean; + plugin: MakeMDPlugin }> = (props) => { const [mode, setMode] = useState(props.mobile ? 0 : 1); const [colorMode, setColorMode] = useState<{ @@ -53,8 +54,8 @@ export const InlineMenuComponent: React.FC<{ const end = cm.state.selection.main.to; const insertChars = cm.state.sliceDoc(end - 1, end) == cm.state.lineBreak - ? window.make.settings.menuTriggerChar - : cm.state.lineBreak + window.make.settings.menuTriggerChar; + ? props.plugin.settings.menuTriggerChar + : cm.state.lineBreak + props.plugin.settings.menuTriggerChar; cm.dispatch({ changes: { from: end, @@ -135,7 +136,7 @@ export const InlineMenuComponent: React.FC<{ aria-label={!platformIsMobile() ? t.commands.image : undefined} onMouseDown={() => { const view = getActiveMarkdownView(); - window.make.app.commands.commands[ + props.plugin.app.commands.commands[ "editor:attach-file" ].editorCallback(view.editor, view); }} @@ -146,7 +147,7 @@ export const InlineMenuComponent: React.FC<{ aria-label={!platformIsMobile() ? t.commands.toggleKeyboard : undefined} onMouseDown={() => { const view = getActiveMarkdownView(); - window.make.app.commands.commands[ + props.plugin.app.commands.commands[ "editor:toggle-keyboard" ].editorCallback(view.editor, view); }} @@ -222,7 +223,7 @@ export const InlineMenuComponent: React.FC<{ > ); })} - {window.make.settings.inlineStylerColors ? ( + {props.plugin.settings.inlineStylerColors ? ( <>
({ - create: getCursorTooltips, +const cursorTooltipField = (plugin: MakeMDPlugin) => StateField.define({ + create: getCursorTooltips(plugin), update(tooltips, tr) { if (!tr.docChanged && !tr.selection) return tooltips; - return getCursorTooltips(tr.state); + return getCursorTooltips(plugin)(tr.state); }, provide: (f) => showTooltip.computeN([f], (state) => state.field(f)), @@ -19,8 +19,9 @@ import { EditorState } from "@codemirror/state"; import { InlineMenuComponent } from "cm-extensions/inlineStylerView/InlineMenu"; import { oMarks } from "cm-extensions/markSans/obsidianSyntax"; import { rangeIsMark, expandRange } from "./marks"; +import MakeMDPlugin from "main"; -function getCursorTooltips(state: EditorState): readonly Tooltip[] { +const getCursorTooltips = (plugin: MakeMDPlugin) => (state: EditorState): readonly Tooltip[] => { return state.selection.ranges .filter((range) => !range.empty) .map((range) => { @@ -41,6 +42,7 @@ function getCursorTooltips(state: EditorState): readonly Tooltip[] { reactElement.render( <> { + let evt = new CustomEvent(eventTypes.loadPortal, { + detail: { id, el, view }, + }); + activeWindow.dispatchEvent(evt); +} + export const focusFlowEditor = (id: string, top: boolean) => { let evt = new CustomEvent(eventTypes.focusPortal, { detail: { id, parent: false, top }, diff --git a/src/main.ts b/src/main.ts index fb5f0ce..085edc2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,6 +28,7 @@ import { import { eventTypes, FocusPortalEvent, + LoadPortalEvent, OpenFilePortalEvent, SpawnPortalEvent, VaultChange, @@ -42,6 +43,7 @@ import "css/makerMode.css"; import { cmExtensions } from "cm-extensions/cmExtensions"; import { focusPortal, + loadFlowEditorByDOM, loadFlowEditorsForLeaf, openFileFromPortal, spawnNewPortal, @@ -175,6 +177,7 @@ export default class MakeMDPlugin extends Plugin { }); window.addEventListener(eventTypes.spawnPortal, this.spawnPortal); + window.addEventListener(eventTypes.loadPortal, this.loadPortal); window.addEventListener(eventTypes.focusPortal, this.focusPortal); window.addEventListener( eventTypes.openFilePortal, @@ -211,10 +214,9 @@ export default class MakeMDPlugin extends Plugin { this.registerEditorSuggest(new MakeMenu(this.app, this)); this.registerEditorSuggest(new StickerMenu(this.app, this)); if (platformIsMobile() && this.settings.mobileMakeBar) - loadStylerIntoContainer(app.mobileToolbar.containerEl); + loadStylerIntoContainer(app.mobileToolbar.containerEl, this); } async onload() { - window.make = this; addIcon("mk-logo", mkLogo); console.log("Loading Make.md"); // Load Settings @@ -230,6 +232,9 @@ export default class MakeMDPlugin extends Plugin { openFileFromPortal(e: OpenFilePortalEvent) { openFileFromPortal(this, e); } + loadPortal(e: LoadPortalEvent) { + loadFlowEditorByDOM(this, e.detail.el, e.detail.view, e.detail.id); + } spawnPortal(e: SpawnPortalEvent) { spawnNewPortal(this, e); } @@ -299,6 +304,7 @@ export default class MakeMDPlugin extends Plugin { onunload() { console.log("Unloading Make.md"); window.removeEventListener(eventTypes.spawnPortal, this.spawnPortal); + window.removeEventListener(eventTypes.loadPortal, this.loadPortal); window.removeEventListener(eventTypes.focusPortal, this.focusPortal); window.removeEventListener( eventTypes.openFilePortal, diff --git a/src/types/types.ts b/src/types/types.ts index 44e7076..c7b7407 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1,5 +1,6 @@ import { TFolder, TFile } from "obsidian"; import { UniqueIdentifier } from "@dnd-kit/core"; +import { EditorView } from '@codemirror/view' export interface SectionTree { section: string; @@ -37,6 +38,7 @@ export const eventTypes = { updateSections: "mkmd-update-sections", settingsChanged: "mkmd-settings-changed", spawnPortal: "mkmd-portal-spawn", + loadPortal: "mkmd-portal-load", openFilePortal: "mkmd-portal-file", focusPortal: "mkmd-portal-focus", }; @@ -57,6 +59,14 @@ export class CustomVaultChangeEvent extends Event { }; } +export class LoadPortalEvent extends Event { + detail: { + el: HTMLElement; + view: EditorView; + id: string; + }; +} + export class SpawnPortalEvent extends Event { detail: { el: HTMLElement; diff --git a/src/utils/flowEditor.ts b/src/utils/flowEditor.ts index 9783173..c163c8c 100644 --- a/src/utils/flowEditor.ts +++ b/src/utils/flowEditor.ts @@ -46,7 +46,7 @@ export const getFileFromString = (url: string, source: string) => { const getLineRangeFromRef = ( file: TFile, ref: string | undefined, - app: App + plugin: MakeMDPlugin ): [number | undefined, number | undefined] => { if (!ref) { return [undefined, undefined]; @@ -70,7 +70,7 @@ const getLineRangeFromRef = ( ); const start = - window.make.settings.editorFlowStyle == "classic" + plugin.settings.editorFlowStyle == "classic" ? heading.position.start.line + 1 : heading.position.start.line + 2; if (index < headings.length - 1 && nextIndex != -1) { @@ -82,9 +82,11 @@ const getLineRangeFromRef = ( }; export const loadFlowEditorByDOM = ( + make: MakeMDPlugin, el: HTMLElement, view: EditorView, - id: string + id: string, + ) => { setTimeout(async () => { //wait for el to be attached to the displayed document @@ -114,7 +116,7 @@ export const loadFlowEditorByDOM = ( //@ts-ignore const cm = leaf.view.editor?.cm as EditorView; if (cm && view.dom == cm.dom) { - loadFlowEditorsForLeafForID(cm, leaf, app, id); + loadFlowEditorsForLeafForID(cm, leaf, make, id); } }, app.workspace["rootSplit"]!); }); @@ -123,27 +125,27 @@ export const loadFlowEditorByDOM = ( export const loadFlowEditorsForLeafForID = ( cm: EditorView, leaf: WorkspaceLeaf, - app: App, + make: MakeMDPlugin, id: string ) => { const stateField = cm.state.field(flowEditorInfo, false); if (!stateField) return; const flowInfo = stateField.find((f) => f.id == id); if (flowInfo && flowInfo.expandedState == 2) { - loadFlowEditor(cm, flowInfo, leaf, app); + loadFlowEditor(cm, flowInfo, leaf, make); } }; export const loadFlowEditorsForLeaf = ( cm: EditorView, leaf: WorkspaceLeaf, - app: App + make: MakeMDPlugin, ) => { const stateField = cm.state.field(flowEditorInfo, false); if (!stateField) return; for (let flowInfo of stateField) { if (flowInfo.expandedState == 2 && flowInfo.embed <= 1) { - loadFlowEditor(cm, flowInfo, leaf, app); + loadFlowEditor(cm, flowInfo, leaf, make); } } }; @@ -152,7 +154,7 @@ export const loadFlowEditor = ( cm: EditorView, flowEditorInfo: FlowEditorInfo, leaf: WorkspaceLeaf, - app: App + make: MakeMDPlugin, ) => { const dom = cm.dom.querySelector( "#mk-flow-" + flowEditorInfo.id @@ -163,7 +165,7 @@ export const loadFlowEditor = ( const file = getFileFromString(link, source); if (dom) { if (file) { - const selectiveRange = getLineRangeFromRef(file, ref, app); + const selectiveRange = getLineRangeFromRef(file, ref, make); if (!dom.hasAttribute("ready")) { // dom.empty(); dom.setAttribute("ready", ""); @@ -185,7 +187,7 @@ export const loadFlowEditor = ( e.stopImmediatePropagation(); //@ts-ignore await app.fileManager.createNewMarkdownFile(app.vault.getRoot(), link); - loadFlowEditor(cm, flowEditorInfo, leaf, app); + loadFlowEditor(cm, flowEditorInfo, leaf, make); }; createDiv.setText(`"${link}" ` + t.labels.noFile); createDiv.addEventListener("click", createFile); diff --git a/src/utils/icons.ts b/src/utils/icons.ts index 873e34c..d009a78 100644 --- a/src/utils/icons.ts +++ b/src/utils/icons.ts @@ -187,3 +187,10 @@ export const markIconSet: Record = { `, }; + +/** + * Some UI Icons based on + * Hero Icons + * https://github.com/tailwindlabs/heroicons + * tailwindlabs + **/