Skip to content

Commit

Permalink
removed global var, added icon attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-cen committed Dec 8, 2022
1 parent eb28e90 commit c1fdc86
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 56 deletions.
67 changes: 40 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
Expand All @@ -8818,15 +8825,15 @@ 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];
}
return [start2, sections2[sections2.length - 1].position.end.line + 1];
}
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)
Expand All @@ -8850,30 +8857,30 @@ 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);
const source = (_a2 = leaf.view.file) == null ? void 0 : _a2.path;
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]);
Expand All @@ -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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -23633,15 +23641,15 @@ 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"] }
}), /* @__PURE__ */ bn.createElement("div", {
"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"] }
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -23756,9 +23765,9 @@ function getCursorTooltips(state) {
}
};
});
}
function cursorTooltip() {
return cursorTooltipField;
};
function cursorTooltip(plugin) {
return cursorTooltipField(plugin);
}

// src/cm-extensions/flowEditor/flowViewUpdates.ts
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/cm-extensions/cmExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/cm-extensions/flowEditor/flowEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 8 additions & 7 deletions src/cm-extensions/inlineStylerView/InlineMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ 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(
<InlineMenuComponent mobile={true} activeMarks={[]}></InlineMenuComponent>
<InlineMenuComponent mobile={true} activeMarks={[]} plugin={plugin}></InlineMenuComponent>
);
};

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<{
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}}
Expand All @@ -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);
}}
Expand Down Expand Up @@ -222,7 +223,7 @@ export const InlineMenuComponent: React.FC<{
></Mark>
);
})}
{window.make.settings.inlineStylerColors ? (
{props.plugin.settings.inlineStylerColors ? (
<>
<div className="mk-divider"></div>
<div
Expand Down
14 changes: 8 additions & 6 deletions src/cm-extensions/inlineStylerView/inlineStyler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import React from "react";
import ReactDOM from "react-dom";
import { createRoot } from "react-dom/client";

const cursorTooltipField = StateField.define<readonly Tooltip[]>({
create: getCursorTooltips,
const cursorTooltipField = (plugin: MakeMDPlugin) => StateField.define<readonly Tooltip[]>({
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)),
Expand All @@ -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) => {
Expand All @@ -41,6 +42,7 @@ function getCursorTooltips(state: EditorState): readonly Tooltip[] {
reactElement.render(
<>
<InlineMenuComponent
plugin={plugin}
cm={view}
activeMarks={activeMarks}
mobile={false}
Expand All @@ -53,6 +55,6 @@ function getCursorTooltips(state: EditorState): readonly Tooltip[] {
});
}

export function cursorTooltip() {
return cursorTooltipField;
export function cursorTooltip(plugin: MakeMDPlugin) {
return cursorTooltipField(plugin);
}
12 changes: 12 additions & 0 deletions src/dispatch/flowDispatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TFile } from "obsidian";
import { SpawnPortalEvent, eventTypes, PortalType } from "types/types";
import { EditorView } from '@codemirror/view'

export const createFlowEditorInElement = (
id: string,
Expand All @@ -15,6 +16,17 @@ export const createFlowEditorInElement = (
activeWindow.dispatchEvent(evt);
};

export const loadFlowEditorByDOM = (
el: HTMLElement,
view: EditorView,
id: string,
) => {
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 },
Expand Down
Loading

0 comments on commit c1fdc86

Please sign in to comment.