diff --git a/ea-scripts/Auto Draw for Pen.md b/ea-scripts/Auto Draw for Pen.md new file mode 100644 index 0000000..433fd91 --- /dev/null +++ b/ea-scripts/Auto Draw for Pen.md @@ -0,0 +1,52 @@ +/* +Automatically switches between the select and draw tools, based on whether a pen is being used. + +1. Choose the select tool +2. Hover, then use the pen to draw, move it away to return to select mode +*This is based on pen hover status, so will only work if your pen supports hover!* + +**Note:** Run this script *once*, it will stay active until the Obsidian is closed. *(I'd recommend you run this at startup via a commander plugin macro, after a short delay)* + +Compatible with my *Hardware eraser support* script + +```javascript +*/ +(function() { + 'use strict'; + + let promise + let timeout + function pointerSwitch(e) { + const pen = document.querySelector('[data-testid="toolbar-freedraw"]' ) + const sel = document.querySelector('[data-testid="toolbar-selection"]') + + if (e.pointerType === 'pen') { + if (sel.checked) { + pen.click() + } + + if (timeout) clearTimeout(timeout) + + function setTimeoutX(a,b) { + timeout = setTimeout(a,b) + return timeout + } + + function revert() { + if (pen.checked) { + sel.click() + } + } + + promise = new Promise(resolve => setTimeoutX(resolve, 500)) + promise.then(() => revert()) + } + } + + function test(e) { + console.log('aa') + } + + window.addEventListener('pointermove', pointerSwitch, { capture: true }) + window.addEventListener('pointerleave', test, { capture: true }) +})(); \ No newline at end of file diff --git a/ea-scripts/Auto Draw for Pen.svg b/ea-scripts/Auto Draw for Pen.svg new file mode 100644 index 0000000..f9f6ef8 --- /dev/null +++ b/ea-scripts/Auto Draw for Pen.svg @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ea-scripts/Hardware Eraser Support.md b/ea-scripts/Hardware Eraser Support.md new file mode 100644 index 0000000..cfcd675 --- /dev/null +++ b/ea-scripts/Hardware Eraser Support.md @@ -0,0 +1,66 @@ +/* +Adds support for pen inversion/hardware erasers. + +Simply use the eraser on a supported pen, and it will erase. Your previous tool will be restored when the eraser leaves the screen. +(Tested with a surface pen, but should work with all windows ink devices, and probably others) + +**Note:** Run this script *once*, it will stay active until the Obsidian is closed. *(I'd recommend you run this at startup via a commander plugin macro, after a short delay)* + +Compatible with my *Auto Draw for Pen* script + +```javascript +*/ +(function() { + 'use strict'; + + let activated + let revert + function handlePointer(e) { + const isEraser = e.pointerType === 'pen' && e.buttons & 32 + if (!activated && isEraser) { + //Store previous tool + const btns = document.querySelectorAll('input.ToolIcon_type_radio') + for (const i in btns) { + if (btns[i]?.checked) { + revert = btns[i] + } + } + + // Activate eraser tool + document.querySelector('[aria-label="Eraser"]')?.click() + activated = true + + // Force Excalidraw to recognize this the same as pen tip + // https://github.com/excalidraw/excalidraw/blob/4a9fac2d1e5c4fac334201ef53c6f5d2b5f6f9f5/src/components/App.tsx#L2945-L2951 + Object.defineProperty(e, 'button', { + value: 0, + writable: false + }); + } + // Keep on eraser! + if (isEraser && activated) { + document.querySelector('[aria-label="Eraser"]')?.click() + } + if (activated && !isEraser) { + // Revert tool on release + revert.click() + activated = false + + // Force delete "limbo" elements + // This doesn't happen on the web app + // It's a bug caused by switching to eraser during a stroke + ea.setView("active"); + var del = [] + for (const i in ea.getViewElements()) { + const element = ea.getViewElements()[i]; + if (element.opacity === 20) { + del.push(element) + } + } + ea.deleteViewElements(del) + } + } + + window.addEventListener('pointerdown', handlePointer, { capture: true }) + window.addEventListener('pointermove', handlePointer, { capture: true }) +})(); \ No newline at end of file diff --git a/ea-scripts/Hardware Eraser Support.svg b/ea-scripts/Hardware Eraser Support.svg new file mode 100644 index 0000000..8b76829 --- /dev/null +++ b/ea-scripts/Hardware Eraser Support.svg @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/ea-scripts/README.md b/ea-scripts/README.md index a548d21..f39e8db 100644 --- a/ea-scripts/README.md +++ b/ea-scripts/README.md @@ -75,3 +75,5 @@ Open the script you are interested in and save it to your Obsidian Vault includi |[Toggle Fullscreen on Mobile](Toggle%20Fullscreen%20on%20Mobile.md)|Hides Obsidian workspace leaf padding and header (based on option in settings, default is "hide header" = false) which will take Excalidraw to full screen. ⚠ Note that if the header is not visible, it will be very difficult to invoke the command palette to end full screen. Only hide the header if you have a keyboard or you've practiced opening command palette!|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/ea-toggle-fullscreen.jpg)|[@zsviczian](https://github.com/zsviczian)| |[Transfer TextElements to Excalidraw markdown metadata](Transfer%20TextElements%20to%20Excalidraw%20markdown%20metadata.md)|The script will delete the selected text elements from the canvas and will copy the text from these text elements into the Excalidraw markdown file as metadata. This means, that the text will no longer be visible in the drawing, however you will be able to search for the text in Obsidian and find the drawing containing this image.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-text-to-metadata.jpg)|[@zsviczian](https://github.com/zsviczian)| |[Zoom to Fit Selected Elements](Zoom%20to%20Fit%20Selected%20Elements.md)|Similar to Excalidraw standard SHIFT+2 feature: Zoom to fit selected elements, but with the ability to zoom to 1000%. Inspiration: [#272](https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/272)||[@zsviczian](https://github.com/zsviczian)| +|[Hardware Eraser Suppoer](Hardware%20Eraser%20Support.md)|Allows the use of pen inversion/hardware erasers on supported pens.|[@threethan](https://github.com/threethan)| +|[Hardware Eraser Suppoer](Auto%20Draw%20for%20Pen.md)|Automatically switched from the Select tool to the Draw tool when a pen is hovered, and then back.|[@threethan](https://github.com/threethan)| \ No newline at end of file diff --git a/ea-scripts/index-new.md b/ea-scripts/index-new.md index 507c59d..e598976 100644 --- a/ea-scripts/index-new.md +++ b/ea-scripts/index-new.md @@ -79,6 +79,8 @@ I would love to include your contribution in the script library. If you have a s |
|[[#Text Arch]]| |
|[[#Uniform Size]]| |
|[[#Zoom to Fit Selected Elements]]| +|
|[[#Hardware Eraser Support]]| +|
|[[#Auto Draw for Pen]]| ## Add Connector Point ```excalidraw-script-install @@ -392,3 +394,16 @@ https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Zoom%20to%20Fit%20Selected%20Elements.md ```
Author@zsviczian
SourceFile on GitHub
DescriptionSimilar to Excalidraw standard SHIFT+2 feature: Zoom to fit selected elements, but with the ability to zoom to 1000%. Inspiration: [#272](https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/272)
+ +## Hardware Eraser Support +```excalidraw-script-install +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Hardware%20Eraser%20Support.md +``` +
Author@threethan
SourceFile on GitHub
DescriptionAllows you to use inversion, aka hardware eraser, on supported pens.
+ + +## Auto Draw for Pen +```excalidraw-script-install +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Auto%20Draw%20for%20Pen.md +``` +
Author@threethan
SourceFile on GitHub
DescriptionAutomatically switches from select mode to drawing mode when hovering a pen, and then back.
\ No newline at end of file