mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2025-08-06 05:46:28 +00:00
1.4.8-beta-2
This commit is contained in:
@@ -6,7 +6,7 @@ import { ExcalidrawData } from "./ExcalidrawData";
|
||||
import ExcalidrawView, { ExportSettings } from "./ExcalidrawView";
|
||||
import { tex2dataURL } from "./LaTeX";
|
||||
import ExcalidrawPlugin from "./main";
|
||||
import { debug, getImageSize, svgToBase64 } from "./Utils";
|
||||
import {getImageSize, svgToBase64 } from "./Utils";
|
||||
|
||||
export declare type MimeType = "image/svg+xml" | "image/png" | "image/jpeg" | "image/gif" | "application/octet-stream";
|
||||
|
||||
@@ -27,9 +27,8 @@ export class EmbeddedFilesLoader {
|
||||
size: {height: number, width: number},
|
||||
}> {
|
||||
if(!this.plugin || !file) return null;
|
||||
//debug("EmbeddedFileLoader.getObsidianImage start file:'" + file.path + "'");
|
||||
//to block infinite loop of recursive loading of images
|
||||
if(this.processedFiles.has(file.path)) {
|
||||
if((file.extension==="md" || file.extension === "excalidraw") && this.processedFiles.has(file.path)) {
|
||||
new Notice("Stopped loading infinite image embed loop at repeated instance of " + file.path,6000);
|
||||
return null;
|
||||
}
|
||||
@@ -48,20 +47,14 @@ export class EmbeddedFilesLoader {
|
||||
withTheme: false
|
||||
};
|
||||
this.plugin.ea.reset();
|
||||
//const png:Blob = await this.plugin.ea.createPNG(file.path,3,this);
|
||||
//const dURL = await getDataURL(await png.arrayBuffer());
|
||||
const svg = await this.plugin.ea.createSVG(file.path,true,exportSettings,this);
|
||||
const dURL = "data:image/svg+xml;base64," + btoa(new XMLSerializer().serializeToString(svg));
|
||||
//const dURL = svgToBase64(svg.outerHTML) as DataURL;
|
||||
//debug("EmbeddedFileLoader.getObsidianImage.getExcalidrawSVG start file:'" + file.path + "'", {png,dURL});
|
||||
// const div = document.body.createDiv(); div.appendChild(svg); trying to hack
|
||||
const dURL = svgToBase64(svg.outerHTML) as DataURL;
|
||||
return dURL as DataURL;
|
||||
}
|
||||
|
||||
const excalidrawSVG = isExcalidrawFile
|
||||
? await getExcalidrawSVG()
|
||||
: null;
|
||||
//let mimeType:MimeType = "image/png";
|
||||
let mimeType:MimeType = "image/svg+xml";
|
||||
if (!isExcalidrawFile) {
|
||||
switch (file.extension) {
|
||||
@@ -73,9 +66,8 @@ export class EmbeddedFilesLoader {
|
||||
default: mimeType = "application/octet-stream";
|
||||
}
|
||||
}
|
||||
const dataURL = excalidrawSVG ?? (file.extension==="svg" ? await getSVGData(app,file) : await getDataURL(ab));
|
||||
const dataURL = excalidrawSVG ?? (file.extension==="svg" ? await getSVGData(app,file) : await getDataURL(ab,mimeType));
|
||||
const size = await getImageSize(excalidrawSVG??app.vault.getResourcePath(file));
|
||||
debug ("EmbeddedFileLoader.getObsidianImage finish file:'" + file.path + "'",{mimeType, dataURL,size});
|
||||
return {
|
||||
mimeType: mimeType,
|
||||
fileId: await generateIdFromFile(ab),
|
||||
@@ -91,7 +83,6 @@ export class EmbeddedFilesLoader {
|
||||
addFiles:Function,
|
||||
sourcePath:string
|
||||
) {
|
||||
//debug("EmbeddedFileLoader.loadSceneFiles start");
|
||||
const app = this.plugin.app;
|
||||
let entries = excalidrawData.getFileEntries();
|
||||
let entry;
|
||||
@@ -99,9 +90,7 @@ export class EmbeddedFilesLoader {
|
||||
while(!(entry = entries.next()).done) {
|
||||
const file = app.metadataCache.getFirstLinkpathDest(entry.value[1],sourcePath);
|
||||
if(file && file instanceof TFile) {
|
||||
//debug("EmbeddedFileLoader.loadSceneFiles topOfWhile file:'" + file.path + "'");
|
||||
const data = await this.getObsidianImage(file);
|
||||
//debug("EmbeddedFileLoader.loadSceneFiles dataLoaded file:'" + file.path + "'");
|
||||
if(data) {
|
||||
files.push({
|
||||
mimeType : data.mimeType,
|
||||
@@ -132,10 +121,8 @@ export class EmbeddedFilesLoader {
|
||||
}
|
||||
|
||||
try { //in try block because by the time files are loaded the user may have closed the view
|
||||
//debug("EmbeddedFileLoader.loadSceneFiles addFiles");
|
||||
addFiles(files,view);
|
||||
} catch(e) {
|
||||
//debug("EmbeddedFileLoader.loadSceneFiles addFiles error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,7 +132,7 @@ const getSVGData = async (app: App, file: TFile): Promise<DataURL> => {
|
||||
return svgToBase64(svg) as DataURL;
|
||||
}
|
||||
|
||||
const getDataURL = async (file: ArrayBuffer): Promise<DataURL> => {
|
||||
const getDataURL = async (file: ArrayBuffer,mimeType: string): Promise<DataURL> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
@@ -153,7 +140,7 @@ const getDataURL = async (file: ArrayBuffer): Promise<DataURL> => {
|
||||
resolve(dataURL);
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsDataURL(new Blob([new Uint8Array(file)]));
|
||||
reader.readAsDataURL(new Blob([new Uint8Array(file)],{type:'mimeType'}));
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
VIEW_TYPE_EXCALIDRAW,
|
||||
MAX_IMAGE_SIZE,
|
||||
} from "./constants";
|
||||
import { debug, embedFontsInSVG, getPNG, getSVG, scaleLoadedImage, wrapText } from "./Utils";
|
||||
import { embedFontsInSVG, getPNG, getSVG, scaleLoadedImage, wrapText } from "./Utils";
|
||||
import { AppState } from "@zsviczian/excalidraw/types/types";
|
||||
import { EmbeddedFilesLoader } from "./EmbeddedFileLoader";
|
||||
import { tex2dataURL } from "./LaTeX";
|
||||
@@ -346,12 +346,10 @@ export async function initExcalidrawAutomate(plugin: ExcalidrawPlugin):Promise<E
|
||||
exportSettings?:ExportSettings,
|
||||
loader:EmbeddedFilesLoader = new EmbeddedFilesLoader(this.plugin)
|
||||
):Promise<SVGSVGElement> {
|
||||
//debug("ExcalidrawAutomate.createSVG start file:'" + templatePath + "'");
|
||||
const automateElements = this.getElements();
|
||||
const template = templatePath ? (await getTemplate(this.plugin,templatePath,true,loader)) : null;
|
||||
let elements = template ? template.elements : [];
|
||||
elements = elements.concat(automateElements);
|
||||
//debug("ExcalidrawAutomate.createSVG fileLoaded file:'" + templatePath + "', template",template);
|
||||
const svg = await getSVG(
|
||||
{//createDrawing
|
||||
type: "excalidraw",
|
||||
@@ -369,7 +367,6 @@ export async function initExcalidrawAutomate(plugin: ExcalidrawPlugin):Promise<E
|
||||
withTheme: (exportSettings === undefined) ? plugin.settings.exportWithTheme : exportSettings.withTheme
|
||||
}
|
||||
)
|
||||
//debug("ExcalidrawAutomate.createSVG SVG ready",(embedFont ? embedFontsInSVG(svg) : svg));
|
||||
return embedFont ? embedFontsInSVG(svg) : svg;
|
||||
},
|
||||
async createPNG(
|
||||
@@ -866,7 +863,6 @@ async function getTemplate(
|
||||
const app = plugin.app;
|
||||
const vault = app.vault;
|
||||
const templatePath = normalizePath(fileWithPath);
|
||||
//debug("ExcalidrawAutomate.getTemplate start file:'" + templatePath + "'");
|
||||
const file = app.metadataCache.getFirstLinkpathDest(templatePath,'');
|
||||
if(file && file instanceof TFile) {
|
||||
const data = (await vault.read(file)).replaceAll("\r\n","\n").replaceAll("\r","\n");
|
||||
@@ -890,9 +886,7 @@ async function getTemplate(
|
||||
|
||||
let scene = excalidrawData.scene;
|
||||
if(loadFiles) {
|
||||
//debug("ExcalidrawAutomate.getTemplate loadFiles file:'" + templatePath + "'");
|
||||
await loader.loadSceneFiles(excalidrawData, null, (fileArray:any, view:any)=>{
|
||||
//debug("ExcalidrawAutomate.getTemplate addFiles file:'" + templatePath + "'");
|
||||
if(!fileArray) return;
|
||||
for(const f of fileArray) {
|
||||
excalidrawData.scene.files[f.id] = f;
|
||||
@@ -902,7 +896,6 @@ async function getTemplate(
|
||||
},templatePath);
|
||||
}
|
||||
|
||||
//debug("ExcalidrawAutomate.getTemplate return elements,appState,frontmatter,files",scene.elements,scene.appState,data.substring(0,trimLocation),scene.files);
|
||||
return {
|
||||
elements: scene.elements,
|
||||
appState: scene.appState,
|
||||
|
||||
@@ -31,10 +31,10 @@ import {
|
||||
IMAGE_TYPES
|
||||
} from './constants';
|
||||
import ExcalidrawPlugin from './main';
|
||||
import {ExcalidrawAutomate, repositionElementsToCursor} from './ExcalidrawAutomate';
|
||||
import { repositionElementsToCursor} from './ExcalidrawAutomate';
|
||||
import { t } from "./lang/helpers";
|
||||
import { ExcalidrawData, REG_LINKINDEX_HYPERLINK, REGEX_LINK } from "./ExcalidrawData";
|
||||
import { checkAndCreateFolder, debug, download, embedFontsInSVG, getIMGFilename, getNewOrAdjacentLeaf, getNewUniqueFilepath, getPNG, getSVG, rotatedDimensions, scaleLoadedImage, splitFolderAndFilename, svgToBase64, viewportCoordsToSceneCoords } from "./Utils";
|
||||
import { checkAndCreateFolder, download, embedFontsInSVG, getIMGFilename, getNewOrAdjacentLeaf, getNewUniqueFilepath, getPNG, getSVG, rotatedDimensions, scaleLoadedImage, splitFolderAndFilename, svgToBase64, viewportCoordsToSceneCoords } from "./Utils";
|
||||
import { Prompt } from "./Prompt";
|
||||
import { ClipboardData } from "@zsviczian/excalidraw/types/clipboard";
|
||||
import { updateEquation } from "./LaTeX";
|
||||
@@ -57,7 +57,6 @@ export interface ExportSettings {
|
||||
const REG_LINKINDEX_INVALIDCHARS = /[<>:"\\|?*]/g;
|
||||
|
||||
export const addFiles = (files:any, view: ExcalidrawView) => {
|
||||
//debug("ExcalidrawView.addFiles start file:'"+view.file.path+"'");
|
||||
if(files.length === 0) return;
|
||||
const [dirty, scene] = scaleLoadedImage(view.getScene(),files);
|
||||
|
||||
@@ -471,7 +470,6 @@ export default class ExcalidrawView extends TextFileView {
|
||||
* @param justloaded - a flag to trigger zoom to fit after the drawing has been loaded
|
||||
*/
|
||||
private async loadDrawing(justloaded:boolean) {
|
||||
//debug("ExcalidrawView.loadDrawing start file:'"+this.file.path+"'");
|
||||
const excalidrawData = this.excalidrawData.scene;
|
||||
this.justLoaded = justloaded;
|
||||
if(this.excalidrawRef) {
|
||||
@@ -491,7 +489,6 @@ export default class ExcalidrawView extends TextFileView {
|
||||
this.excalidrawWrapperRef.current.focus();
|
||||
}
|
||||
const loader = new EmbeddedFilesLoader(this.plugin);
|
||||
//debug("ExcalidrawView.loadDrawing calling loadSceneFiles file:'"+this.file.path+"'");
|
||||
loader.loadSceneFiles(
|
||||
this.excalidrawData,
|
||||
this,
|
||||
|
||||
@@ -213,7 +213,6 @@ export const getAttachmentsFolderAndFilePath = async (app:App, activeViewFilePat
|
||||
}
|
||||
|
||||
export const getSVG = async (scene:any, exportSettings:ExportSettings):Promise<SVGSVGElement> => {
|
||||
//debug("Utils.getSVG enter scene:",scene);
|
||||
try {
|
||||
return await exportToSvg({
|
||||
elements: scene.elements,
|
||||
@@ -270,7 +269,6 @@ export const getImageSize = async (src:string):Promise<{height:number, width:num
|
||||
|
||||
export const scaleLoadedImage = (scene:any, files:any):[boolean,any] => {
|
||||
let dirty = false;
|
||||
//debug("Utils.scaleLoadedImage scene,files", scene,files);
|
||||
for(const f of files) {
|
||||
const [w_image,h_image] = [f.size.width,f.size.height];
|
||||
const imageAspectRatio = f.size.width/f.size.height;
|
||||
@@ -300,5 +298,5 @@ export function getIMGFilename(path:string,extension:string):string {
|
||||
return path.substring(0,path.lastIndexOf('.')) + '.' + extension;
|
||||
}
|
||||
|
||||
export const debug = console.log.bind(window.console);
|
||||
//export const debug = console.log.bind(window.console);
|
||||
//export const debug = function(){};
|
||||
Reference in New Issue
Block a user