-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c9502
commit 3432cfa
Showing
4 changed files
with
108 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Notice } from 'obsidian'; | ||
|
||
export function copyImageToClipboard(svg: SVGElement) { | ||
const canvas = createCanvas(svg); | ||
const img = generateImage(svg, canvas, () => { | ||
canvas.toBlob((blob: any) => { | ||
const item = new ClipboardItem({ "image/png": blob }); | ||
navigator.clipboard.write([item]); | ||
new Notice('Screenshot copied to the clipboard.') | ||
}); | ||
}); | ||
} | ||
|
||
function createCanvas(svg: SVGElement): HTMLCanvasElement { | ||
const canvas = document.createElement("canvas"); | ||
canvas.width = svg.clientWidth; | ||
canvas.height = svg.clientHeight; | ||
console.log(canvas); | ||
return canvas; | ||
} | ||
|
||
function generateImage(svg: SVGElement, canvas: HTMLCanvasElement, callback: () => void): HTMLImageElement { | ||
var ctx = canvas.getContext("2d"); | ||
return drawInlineSVG(ctx, svg, callback); | ||
} | ||
|
||
function drawInlineSVG(ctx: CanvasRenderingContext2D, svg: SVGElement, callback: () => void): HTMLImageElement { | ||
|
||
// get svg data | ||
var xml = new XMLSerializer().serializeToString(svg); | ||
|
||
// make it base64 | ||
var svg64 = btoa(xml); | ||
var b64Start = 'data:image/svg+xml;base64,'; | ||
|
||
// prepend a "header" | ||
var image64 = b64Start + svg64; | ||
|
||
const img = new Image(); | ||
// set it as the source of the img element | ||
img.onload = function() { | ||
// draw the image onto the canvas | ||
ctx.drawImage(img, 0, 0); | ||
callback(); | ||
} | ||
img.src = image64; | ||
return img; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export function createSVG(containerEl: HTMLElement): SVGElement { | ||
removeExistingSVG(); | ||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') as unknown as SVGElement; | ||
svg.id = 'markmap'; | ||
svg.setAttr('style', 'height: 100%; width: 100%;'); | ||
const container = containerEl.children[1]; | ||
const style = document.createElement('style'); | ||
const { color } = getComputedCss(containerEl); | ||
style.innerHTML = `#markmap div { | ||
color: ${color}; | ||
line-height: 1em; | ||
}`; | ||
svg.appendChild(style); | ||
container.appendChild(svg); | ||
return svg; | ||
} | ||
|
||
export function removeExistingSVG() { | ||
const existing = document.getElementById('markmap'); | ||
if(existing) { | ||
existing.parentElement.removeChild(existing); | ||
} | ||
} | ||
|
||
export function getComputedCss(el: HTMLElement) { | ||
const computed = getComputedStyle(el); | ||
const color = computed.getPropertyValue('--text-normal'); | ||
const font = `1em ${computed.getPropertyValue('--default-font')}`; | ||
return { color, font }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters