diff --git a/package.json b/package.json index 9475ca1..847065a 100644 --- a/package.json +++ b/package.json @@ -1,40 +1,40 @@ -{ - "name": "obsidian-excalidraw-plugin", - "version": "1.3.21", - "description": "This is an Obsidian.md plugin that lets you view and edit Excalidraw drawings", - "main": "main.js", - "scripts": { - "dev": "cross-env NODE_ENV=development rollup --config rollup.config.js -w", - "build": "cross-env NODE_ENV=production rollup --config rollup.config.js" - }, - "keywords": [], - "author": "", - "license": "MIT", - "dependencies": { - "@zsviczian/excalidraw": "0.9.0-obsidian-image-support-11", - "monkey-around": "^2.2.0", - "react": "^17.0.2", - "react-dom": "^17.0.2", - "react-scripts": "^1.1.5", - "roughjs": "4.4.1" - }, - "devDependencies": { - "@babel/core": "^7.15.5", - "@babel/preset-env": "^7.15.6", - "@babel/preset-react": "^7.14.5", - "@rollup/plugin-babel": "^5.3.0", - "@rollup/plugin-commonjs": "^21.0.0", - "@rollup/plugin-node-resolve": "^13.0.5", - "@rollup/plugin-replace": "^2.4.2", - "@rollup/plugin-typescript": "^8.2.5", - "@types/node": "^15.12.4", - "@types/react-dom": "^17.0.9", - "cross-env": "^7.0.3", - "nanoid": "^3.1.23", - "obsidian": "^0.12.16", - "rollup": "^2.52.3", - "rollup-plugin-visualizer": "^5.5.2", - "tslib": "^2.3.1", - "typescript": "^4.4.3" - } -} +{ + "name": "obsidian-excalidraw-plugin", + "version": "1.3.21", + "description": "This is an Obsidian.md plugin that lets you view and edit Excalidraw drawings", + "main": "main.js", + "scripts": { + "dev": "cross-env NODE_ENV=development rollup --config rollup.config.js -w", + "build": "cross-env NODE_ENV=production rollup --config rollup.config.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "dependencies": { + "@zsviczian/excalidraw": "0.9.0-obsidian-image-support-11", + "monkey-around": "^2.2.0", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-scripts": "^1.1.5", + "roughjs": "4.4.1" + }, + "devDependencies": { + "@babel/core": "^7.15.5", + "@babel/preset-env": "^7.15.6", + "@babel/preset-react": "^7.14.5", + "@rollup/plugin-babel": "^5.3.0", + "@rollup/plugin-commonjs": "^21.0.0", + "@rollup/plugin-node-resolve": "^13.0.5", + "@rollup/plugin-replace": "^2.4.2", + "@rollup/plugin-typescript": "^8.2.5", + "@types/node": "^15.12.4", + "@types/react-dom": "^17.0.9", + "cross-env": "^7.0.3", + "nanoid": "^3.1.23", + "obsidian": "^0.12.16", + "rollup": "^2.52.3", + "rollup-plugin-visualizer": "^5.5.2", + "tslib": "^2.3.1", + "typescript": "^4.4.3" + } +} diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 8980cb9..16a0f71 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -81,7 +81,6 @@ export default class ExcalidrawView extends TextFileView { private ctrlKeyDown = false; private shiftKeyDown = false; private altKeyDown = false; - private mouseEvent:any = null; id: string = (this.leaf as any).id; @@ -794,6 +793,18 @@ export default class ExcalidrawView extends TextFileView { if (transfer.types?.includes('text/html') || transfer.types?.includes('text/plain')) return 'copy'; } + let viewModeEnabled = false; + const handleLinkClick = () => { + selectedTextElement = getTextElementAtPointer(currentPosition); + if(selectedTextElement) { + const event = new MouseEvent("click", {ctrlKey: true, shiftKey: this.shiftKeyDown, altKey:this.altKeyDown}); + this.handleLinkClick(this,event); + selectedTextElement = null; + } + } + + let mouseEvent:any = null; + const excalidrawDiv = React.createElement( "div", { @@ -834,7 +845,7 @@ export default class ExcalidrawView extends TextFileView { this.plugin.hover.sourcePath = this.file.path; hoverPreviewTarget = this.contentEl; //e.target; this.app.workspace.trigger('hover-link', { - event: this.mouseEvent, + event: mouseEvent, source: VIEW_TYPE_EXCALIDRAW, hoverParent: hoverPreviewTarget, targetEl: hoverPreviewTarget, @@ -865,7 +876,7 @@ export default class ExcalidrawView extends TextFileView { }, onMouseMove: (e:MouseEvent) => { //@ts-ignore - this.mouseEvent = e.nativeEvent; + mouseEvent = e.nativeEvent; }, onMouseOver: (e:MouseEvent) => { clearHoverPreview(); @@ -899,17 +910,9 @@ export default class ExcalidrawView extends TextFileView { onPointerUpdate: (p:any) => { currentPosition = p.pointer; if(hoverPreviewTarget && (Math.abs(hoverPoint.x-p.pointer.x)>50 || Math.abs(hoverPoint.y-p.pointer.y)>50)) clearHoverPreview(); - if(!this.excalidrawRef.current.getAppState().viewModeEnabled) return; - const handleLinkClick = () => { - selectedTextElement = getTextElementAtPointer(p.pointer); - if(selectedTextElement) { - const event = new MouseEvent("click", {ctrlKey: true, shiftKey: this.shiftKeyDown, altKey:this.altKeyDown}); - this.handleLinkClick(this,event); - selectedTextElement = null; - } - } + if(!viewModeEnabled) return; - const buttonDown = !blockOnMouseButtonDown && p.button=="down"; + const buttonDown = !blockOnMouseButtonDown && p.button === "down"; if(buttonDown) { blockOnMouseButtonDown = true; @@ -927,11 +930,12 @@ export default class ExcalidrawView extends TextFileView { timestamp = now; return; } - if (p.button=="up") { + if (p.button === "up") { blockOnMouseButtonDown=false; } }, onChange: (et:ExcalidrawElement[],st:AppState) => { + viewModeEnabled = st.viewModeEnabled; if(this.justLoaded) { this.justLoaded = false; this.zoomToFit(false);