Compare commits

...

1 Commits

Author SHA1 Message Date
Zsolt Viczian
75b9083ec8 WIP 2022-10-09 08:10:58 +02:00
5 changed files with 89 additions and 46 deletions

View File

@@ -37,7 +37,6 @@ import {
TEXT_DISPLAY_PARSED_ICON_NAME, TEXT_DISPLAY_PARSED_ICON_NAME,
FULLSCREEN_ICON_NAME, FULLSCREEN_ICON_NAME,
IMAGE_TYPES, IMAGE_TYPES,
CTRL_OR_CMD,
REG_LINKINDEX_INVALIDCHARS, REG_LINKINDEX_INVALIDCHARS,
KEYCODE, KEYCODE,
LOCAL_PROTOCOL, LOCAL_PROTOCOL,
@@ -71,11 +70,13 @@ import {
getExportPadding, getExportPadding,
getWithBackground, getWithBackground,
hasExportTheme, hasExportTheme,
isVersionNewerThanOther,
scaleLoadedImage, scaleLoadedImage,
setDocLeftHandedMode,
svgToBase64, svgToBase64,
viewportCoordsToSceneCoords, viewportCoordsToSceneCoords,
isCtrlDown,
isShiftDown,
isAltDown,
isMetaDown,
} from "./utils/Utils"; } from "./utils/Utils";
import { getNewOrAdjacentLeaf, getParentOfClass } from "./utils/ObsidianUtils"; import { getNewOrAdjacentLeaf, getParentOfClass } from "./utils/ObsidianUtils";
import { splitFolderAndFilename } from "./utils/FileUtils"; import { splitFolderAndFilename } from "./utils/FileUtils";
@@ -191,10 +192,17 @@ export default class ExcalidrawView extends TextFileView {
private onKeyUp: (e: KeyboardEvent) => void; private onKeyUp: (e: KeyboardEvent) => void;
private onKeyDown:(e: KeyboardEvent) => void; private onKeyDown:(e: KeyboardEvent) => void;
//store key state for view mode link resolution //store key state for view mode link resolution
private metaKeyDown: boolean = false; private lastKeyboardEvent = new KeyboardEvent("keyboard", {
private ctrlKeyDown: boolean = false; key: "",
private shiftKeyDown: boolean = false; code: "",
private altKeyDown: boolean = false; location: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
repeat: false
});
//Obsidian 0.15.0 //Obsidian 0.15.0
public ownerWindow: Window; public ownerWindow: Window;
public ownerDocument: Document; public ownerDocument: Document;
@@ -876,7 +884,7 @@ export default class ExcalidrawView extends TextFileView {
} }
await this.save(false); //in case pasted images haven't been saved yet await this.save(false); //in case pasted images haven't been saved yet
if (this.excalidrawData.hasFile(selectedImage.fileId)) { if (this.excalidrawData.hasFile(selectedImage.fileId)) {
if (ev.altKey) { if (isAltDown(ev)) {
const ef = this.excalidrawData.getFile(selectedImage.fileId); const ef = this.excalidrawData.getFile(selectedImage.fileId);
if ( if (
ef.file.extension === "md" && ef.file.extension === "md" &&
@@ -930,18 +938,18 @@ export default class ExcalidrawView extends TextFileView {
} }
try { try {
if (ev.shiftKey && this.isFullscreen()) { if (isShiftDown(ev) && this.isFullscreen()) {
this.exitFullscreen(); this.exitFullscreen();
} }
if (!file) { if (!file) {
new NewFileActions(this.plugin, linkText, ev.shiftKey, !app.isMobile && ev.metaKey, view).open(); new NewFileActions(this.plugin, linkText, isShiftDown(ev), !app.isMobile && isMetaDown(ev), view).open();
return; return;
} }
const leaf = const leaf =
(!app.isMobile && ((ev.metaKey && this.linksAlwaysOpenInANewPane) || ev.metaKey)) (!app.isMobile && ((isMetaDown(ev) && this.linksAlwaysOpenInANewPane) || isMetaDown(ev)))
//@ts-ignore //@ts-ignore
? app.workspace.openPopoutLeaf() ? app.workspace.openPopoutLeaf()
: (ev.shiftKey || this.linksAlwaysOpenInANewPane) : (isShiftDown(ev) || this.linksAlwaysOpenInANewPane)
? getNewOrAdjacentLeaf(this.plugin, view.leaf) ? getNewOrAdjacentLeaf(this.plugin, view.leaf)
: view.leaf; : view.leaf;
await leaf.openFile(file, subpath ? { active: false, eState: { subpath } } : undefined); //if file exists open file and jump to reference await leaf.openFile(file, subpath ? { active: false, eState: { subpath } } : undefined); //if file exists open file and jump to reference
@@ -1063,18 +1071,25 @@ export default class ExcalidrawView extends TextFileView {
await self.addSlidingPanesListner(); //awaiting this because when using workspaces, onLayoutReady comes too early await self.addSlidingPanesListner(); //awaiting this because when using workspaces, onLayoutReady comes too early
self.addParentMoveObserver(); self.addParentMoveObserver();
const cloneEvent = (e: KeyboardEvent):KeyboardEvent => {
return new KeyboardEvent(e.type,{
key: e.key,
code: e.code,
location: e.location,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
altKey: e.altKey,
metaKey: e.metaKey,
repeat: e.repeat
});
}
self.onKeyUp = (e: KeyboardEvent) => { self.onKeyUp = (e: KeyboardEvent) => {
self.ctrlKeyDown = e[CTRL_OR_CMD]; self.lastKeyboardEvent = cloneEvent(e);
self.shiftKeyDown = e.shiftKey;
self.altKeyDown = e.altKey;
self.metaKeyDown = e.metaKey;
}; };
self.onKeyDown = (e: KeyboardEvent) => { self.onKeyDown = (e: KeyboardEvent) => {
this.ctrlKeyDown = e[CTRL_OR_CMD]; self.lastKeyboardEvent = cloneEvent(e);
this.shiftKeyDown = e.shiftKey;
this.altKeyDown = e.altKey;
this.metaKeyDown = e.metaKey;
}; };
self.ownerWindow.addEventListener("keydown", self.onKeyDown, false); self.ownerWindow.addEventListener("keydown", self.onKeyDown, false);
@@ -1934,7 +1949,7 @@ export default class ExcalidrawView extends TextFileView {
if (!this.getScene || !this.file) { if (!this.getScene || !this.file) {
return; return;
} }
if (ev[CTRL_OR_CMD]) { if (isCtrlDown(ev)) {
const png = await this.png(this.getScene()); const png = await this.png(this.getScene());
if (!png) { if (!png) {
return; return;
@@ -1961,7 +1976,7 @@ export default class ExcalidrawView extends TextFileView {
if (!this.getScene || !this.file) { if (!this.getScene || !this.file) {
return; return;
} }
if (ev[CTRL_OR_CMD]) { if (isCtrlDown(ev)) {
let svg = await this.svg(this.getScene()); let svg = await this.svg(this.getScene());
if (!svg) { if (!svg) {
return null; return null;
@@ -2676,7 +2691,7 @@ export default class ExcalidrawView extends TextFileView {
this.exitFullscreen(); this.exitFullscreen();
} }
if (e[CTRL_OR_CMD] && !e.shiftKey && !e.altKey) { if (isCtrlDown(e) && !isShiftDown(e) && !isAltDown(e)) {
showHoverPreview(); showHoverPreview();
} }
}, },
@@ -2684,10 +2699,10 @@ export default class ExcalidrawView extends TextFileView {
//onClick: (e: MouseEvent): any => { //onClick: (e: MouseEvent): any => {
//to onPointerDown so touch events also open links on the iPad (with a keyboard) //to onPointerDown so touch events also open links on the iPad (with a keyboard)
onPointerDown: (e: PointerEvent) => { onPointerDown: (e: PointerEvent) => {
if (!(e[CTRL_OR_CMD]||e.metaKey)) { if ( !(isCtrlDown(e) || isMetaDown(e)) ) {
return; return;
} }
if (!this.plugin.settings.allowCtrlClick && !e.metaKey) { if (!this.plugin.settings.allowCtrlClick && !isMetaDown(e)) {
return; return;
} }
//added setTimeout when I changed onClick(e: MouseEvent) to onPointerDown() in 1.7.9. //added setTimeout when I changed onClick(e: MouseEvent) to onPointerDown() in 1.7.9.
@@ -2892,7 +2907,7 @@ export default class ExcalidrawView extends TextFileView {
}; };
//https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/468 //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/468
event[CTRL_OR_CMD] = event.shiftKey || event[CTRL_OR_CMD]; //event[CTRL_OR_CMD] = event.shiftKey || event[CTRL_OR_CMD]; Moved logic to isCtrlDown in Utils
switch (draggable?.type) { switch (draggable?.type) {
case "file": case "file":
if (!onDropHook("file", [draggable.file], null)) { if (!onDropHook("file", [draggable.file], null)) {
@@ -2902,7 +2917,7 @@ export default class ExcalidrawView extends TextFileView {
return false; return false;
} }
if ( if (
event[CTRL_OR_CMD] && isCtrlDown(event) &&
(IMAGE_TYPES.contains(draggable.file.extension) || (IMAGE_TYPES.contains(draggable.file.extension) ||
draggable.file.extension === "md") draggable.file.extension === "md")
) { ) {
@@ -2932,7 +2947,7 @@ export default class ExcalidrawView extends TextFileView {
case "files": case "files":
if (!onDropHook("file", draggable.files, null)) { if (!onDropHook("file", draggable.files, null)) {
(async () => { (async () => {
if (event[CTRL_OR_CMD]) { if (isCtrlDown(event)) {
const ea = this.plugin.ea; const ea = this.plugin.ea;
ea.reset(); ea.reset();
ea.setView(this); ea.setView(this);
@@ -3225,10 +3240,10 @@ export default class ExcalidrawView extends TextFileView {
); );
const useNewLeaf = const useNewLeaf =
event.shiftKey || isShiftDown(event) ||
event[CTRL_OR_CMD] || isCtrlDown(event) ||
this.linksAlwaysOpenInANewPane || this.linksAlwaysOpenInANewPane ||
event.metaKey; isMetaDown(event);
if (useNewLeaf && this.isFullscreen()) { if (useNewLeaf && this.isFullscreen()) {
this.exitFullscreen(); this.exitFullscreen();
@@ -3238,7 +3253,7 @@ export default class ExcalidrawView extends TextFileView {
this.plugin, this.plugin,
linkText, linkText,
useNewLeaf, useNewLeaf,
!app.isMobile && event.metaKey, !app.isMobile && isMetaDown(event),
this, this,
).open(); ).open();
return; return;
@@ -3252,7 +3267,7 @@ export default class ExcalidrawView extends TextFileView {
} else { } else {
try { try {
const leaf = useNewLeaf const leaf = useNewLeaf
? (event.metaKey && !app.isMobile) ? (isMetaDown(event) && !app.isMobile)
//@ts-ignore //@ts-ignore
? app.workspace.openPopoutLeaf() ? app.workspace.openPopoutLeaf()
: getNewOrAdjacentLeaf(this.plugin, this.leaf) : getNewOrAdjacentLeaf(this.plugin, this.leaf)
@@ -3278,7 +3293,7 @@ export default class ExcalidrawView extends TextFileView {
if ( if (
element && element &&
(this.plugin.settings.hoverPreviewWithoutCTRL || (this.plugin.settings.hoverPreviewWithoutCTRL ||
event[CTRL_OR_CMD]) isCtrlDown(event))
) { ) {
mouseEvent = event; mouseEvent = event;
mouseEvent.ctrlKey = true; mouseEvent.ctrlKey = true;

View File

@@ -4,7 +4,7 @@ import {
TFile, TFile,
Vault, Vault,
} from "obsidian"; } from "obsidian";
import { CTRL_OR_CMD, RERENDER_EVENT } from "./Constants"; import { RERENDER_EVENT } from "./Constants";
import { EmbeddedFilesLoader } from "./EmbeddedFileLoader"; import { EmbeddedFilesLoader } from "./EmbeddedFileLoader";
import { createPNG, createSVG } from "./ExcalidrawAutomate"; import { createPNG, createSVG } from "./ExcalidrawAutomate";
import { ExportSettings } from "./ExcalidrawView"; import { ExportSettings } from "./ExcalidrawView";
@@ -19,6 +19,8 @@ import {
getWithBackground, getWithBackground,
hasExportTheme, hasExportTheme,
svgToBase64, svgToBase64,
isCtrlDown,
isMetaDown,
} from "./utils/Utils"; } from "./utils/Utils";
import { isObsidianThemeDark } from "./utils/ObsidianUtils"; import { isObsidianThemeDark } from "./utils/ObsidianUtils";
@@ -211,15 +213,15 @@ const createImageDiv = async (
if(!srcParts) return; if(!srcParts) return;
plugin.openDrawing( plugin.openDrawing(
vault.getAbstractFileByPath(srcParts[1]) as TFile, vault.getAbstractFileByPath(srcParts[1]) as TFile,
ev[CTRL_OR_CMD] isCtrlDown(ev)
? "new-pane" ? "new-pane"
: (ev.metaKey && !app.isMobile) : (isMetaDown(ev) && !app.isMobile)
? "popout-window" ? "popout-window"
: "active-pane", : "active-pane",
true, true,
srcParts[2], srcParts[2],
); );
} //.ctrlKey||ev.metaKey); }
}); });
el.addEventListener(RERENDER_EVENT, async (e) => { el.addEventListener(RERENDER_EVENT, async (e) => {
e.stopPropagation(); e.stopPropagation();
@@ -544,13 +546,13 @@ export const observer = new MutationObserver(async (m) => {
if (src) { if (src) {
plugin.openDrawing( plugin.openDrawing(
vault.getAbstractFileByPath(src) as TFile, vault.getAbstractFileByPath(src) as TFile,
ev[CTRL_OR_CMD] isCtrlDown(ev)
? "new-pane" ? "new-pane"
: (ev.metaKey && !app.isMobile) : (isMetaDown(ev) && !app.isMobile)
? "popout-window" ? "popout-window"
: "active-pane", : "active-pane",
); );
} //.ctrlKey||ev.metaKey); }
}); });
}); });
node.appendChild(div); node.appendChild(div);

View File

@@ -37,7 +37,6 @@ import {
JSON_parse, JSON_parse,
nanoid, nanoid,
DARK_BLANK_DRAWING, DARK_BLANK_DRAWING,
CTRL_OR_CMD,
SCRIPT_INSTALL_CODEBLOCK, SCRIPT_INSTALL_CODEBLOCK,
SCRIPT_INSTALL_FOLDER, SCRIPT_INSTALL_FOLDER,
VIRGIL_FONT, VIRGIL_FONT,
@@ -86,6 +85,7 @@ import {
debug, debug,
isVersionNewerThanOther, isVersionNewerThanOther,
getExportTheme, getExportTheme,
isCtrlDown,
} from "./utils/Utils"; } from "./utils/Utils";
import { getAttachmentsFolderAndFilePath, getNewOrAdjacentLeaf, getParentOfClass, isObsidianThemeDark } from "./utils/ObsidianUtils"; import { getAttachmentsFolderAndFilePath, getNewOrAdjacentLeaf, getParentOfClass, isObsidianThemeDark } from "./utils/ObsidianUtils";
//import { OneOffs } from "./OneOffs"; //import { OneOffs } from "./OneOffs";
@@ -694,7 +694,7 @@ export default class ExcalidrawPlugin extends Plugin {
this.addRibbonIcon(ICON_NAME, t("CREATE_NEW"), async (e) => { this.addRibbonIcon(ICON_NAME, t("CREATE_NEW"), async (e) => {
this.createAndOpenDrawing( this.createAndOpenDrawing(
getDrawingFilename(this.settings), getDrawingFilename(this.settings),
e[CTRL_OR_CMD]?"new-pane":"active-pane", isCtrlDown(e) ? "new-pane" : "active-pane",
); //.ctrlKey||e.metaKey); ); //.ctrlKey||e.metaKey);
}); });

View File

@@ -3,13 +3,14 @@ import { Notice, TFile } from "obsidian";
import * as React from "react"; import * as React from "react";
import { ActionButton } from "./ActionButton"; import { ActionButton } from "./ActionButton";
import { ICONS } from "./ActionIcons"; import { ICONS } from "./ActionIcons";
import { SCRIPT_INSTALL_FOLDER, CTRL_OR_CMD } from "../Constants"; import { SCRIPT_INSTALL_FOLDER } from "../Constants";
import { insertLaTeXToView, search } from "../ExcalidrawAutomate"; import { insertLaTeXToView, search } from "../ExcalidrawAutomate";
import ExcalidrawView, { TextMode } from "../ExcalidrawView"; import ExcalidrawView, { TextMode } from "../ExcalidrawView";
import { t } from "../lang/helpers"; import { t } from "../lang/helpers";
import { ReleaseNotes } from "../dialogs/ReleaseNotes"; import { ReleaseNotes } from "../dialogs/ReleaseNotes";
import { ScriptIconMap } from "../Scripts"; import { ScriptIconMap } from "../Scripts";
import { getIMGFilename } from "../utils/FileUtils"; import { getIMGFilename } from "../utils/FileUtils";
import { isCtrlDown, isShiftDown } from "src/utils/Utils";
declare const PLUGIN_VERSION:string; declare const PLUGIN_VERSION:string;
const dark = '<svg style="stroke:#ced4da;#212529;color:#ced4da;fill:#ced4da" '; const dark = '<svg style="stroke:#ced4da;#212529;color:#ced4da;fill:#ced4da" ';
@@ -466,7 +467,7 @@ export class ToolsPanel extends React.Component<PanelProps, PanelState> {
title={t("INSERT_LINK_TO_ELEMENT")} title={t("INSERT_LINK_TO_ELEMENT")}
action={(e:React.MouseEvent<HTMLButtonElement, MouseEvent>) => { action={(e:React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
this.props.view.copyLinkToSelectedElementToClipboard( this.props.view.copyLinkToSelectedElementToClipboard(
e[CTRL_OR_CMD] ? "group=" : (e.shiftKey ? "area=" : "") isCtrlDown(e) ? "group=" : (isShiftDown(e) ? "area=" : "")
); );
}} }}
icon={ICONS.copyElementLink} icon={ICONS.copyElementLink}

View File

@@ -11,12 +11,12 @@ import {
CASCADIA_FONT, CASCADIA_FONT,
REG_BLOCK_REF_CLEAN, REG_BLOCK_REF_CLEAN,
VIRGIL_FONT, VIRGIL_FONT,
PLUGIN_ID,
FRONTMATTER_KEY_EXPORT_DARK, FRONTMATTER_KEY_EXPORT_DARK,
FRONTMATTER_KEY_EXPORT_TRANSPARENT, FRONTMATTER_KEY_EXPORT_TRANSPARENT,
FRONTMATTER_KEY_EXPORT_SVGPADDING, FRONTMATTER_KEY_EXPORT_SVGPADDING,
FRONTMATTER_KEY_EXPORT_PNGSCALE, FRONTMATTER_KEY_EXPORT_PNGSCALE,
FRONTMATTER_KEY_EXPORT_PADDING, FRONTMATTER_KEY_EXPORT_PADDING,
CTRL_OR_CMD,
} from "../Constants"; } from "../Constants";
import ExcalidrawPlugin from "../main"; import ExcalidrawPlugin from "../main";
import { ExcalidrawElement } from "@zsviczian/excalidraw/types/element/types"; import { ExcalidrawElement } from "@zsviczian/excalidraw/types/element/types";
@@ -24,6 +24,7 @@ import { ExportSettings } from "../ExcalidrawView";
import { compressToBase64, decompressFromBase64 } from "lz-string"; import { compressToBase64, decompressFromBase64 } from "lz-string";
import { getIMGFilename } from "./FileUtils"; import { getIMGFilename } from "./FileUtils";
declare const PLUGIN_VERSION:string; declare const PLUGIN_VERSION:string;
const { const {
@@ -628,6 +629,30 @@ export const errorlog = (data: {}) => {
export const sleep = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export const sleep = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
type GenericEvent = KeyboardEvent | MouseEvent | React.DragEvent | React.PointerEvent | React.MouseEvent;
export const isCtrlDown = (e:GenericEvent):boolean => {
https://stackoverflow.com/questions/44030187/correct-way-to-check-if-any-object-is-a-syntheticevent
return e instanceof Event
? e[CTRL_OR_CMD]
: e.nativeEvent instanceof DragEvent
? e[CTRL_OR_CMD] || isShiftDown(e) // React.DragEvent; //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/468
: e[CTRL_OR_CMD];
}
export const isShiftDown = (e:GenericEvent):boolean => {
return e.shiftKey;
}
export const isAltDown = (e:GenericEvent):boolean => {
return e.altKey;
}
export const isMetaDown = (e:GenericEvent):boolean => {
return e.metaKey;
}
/**REACT 18 /**REACT 18
//see also: https://github.com/zsviczian/obsidian-excalidraw-plugin/commit/b67d70c5196f30e2968f9da919d106ee66f2a5eb //see also: https://github.com/zsviczian/obsidian-excalidraw-plugin/commit/b67d70c5196f30e2968f9da919d106ee66f2a5eb
//https://github.com/zsviczian/obsidian-excalidraw-plugin/commit/cc9d7828c7ee7755c1ef942519c43df32eae249f //https://github.com/zsviczian/obsidian-excalidraw-plugin/commit/cc9d7828c7ee7755c1ef942519c43df32eae249f