This commit is contained in:
Zsolt Viczian
2021-08-02 20:34:56 +02:00
parent 21ff1833a8
commit 2e36d83abc
6 changed files with 40 additions and 25 deletions

View File

@@ -14,7 +14,7 @@ Returns the `id` of the object. The `id` is required when connecting objects wit
### addText() ### addText()
```typescript ```typescript
addText(topX:number, topY:number, text:string, formatting?:{width:number, height:number,textAlign: string, verticalAlign:string, box: boolean, boxPadding: number}):string addText(topX:number, topY:number, text:string, formatting?:{width?:number, height?:number,textAlign?: string, verticalAlign?:string, box?: boolean, boxPadding?: number},id?:string):string;
``` ```
Adds text to the drawing. Adds text to the drawing.

View File

@@ -1,7 +1,7 @@
{ {
"id": "obsidian-excalidraw-plugin", "id": "obsidian-excalidraw-plugin",
"name": "Excalidraw", "name": "Excalidraw",
"version": "1.2.10", "version": "1.2.11",
"minAppVersion": "0.12.0", "minAppVersion": "0.12.0",
"description": "An Obsidian plugin to edit and view Excalidraw drawings", "description": "An Obsidian plugin to edit and view Excalidraw drawings",
"author": "Zsolt Viczian", "author": "Zsolt Viczian",

View File

@@ -55,7 +55,7 @@ export interface ExcalidrawAutomate extends Window {
addRect(topX:number, topY:number, width:number, height:number):string; addRect(topX:number, topY:number, width:number, height:number):string;
addDiamond(topX:number, topY:number, width:number, height:number):string; addDiamond(topX:number, topY:number, width:number, height:number):string;
addEllipse(topX:number, topY:number, width:number, height:number):string; addEllipse(topX:number, topY:number, width:number, height:number):string;
addText(topX:number, topY:number, text:string, formatting?:{width:number, height:number,textAlign: string, verticalAlign:string, box: boolean, boxPadding: number}):string; addText(topX:number, topY:number, text:string, formatting?:{width?:number, height?:number,textAlign?: string, verticalAlign?:string, box?: boolean, boxPadding?: number},id?:string):string;
addLine(points: [[x:number,y:number]]):void; addLine(points: [[x:number,y:number]]):void;
addArrow(points: [[x:number,y:number]],formatting?:{startArrowHead:string,endArrowHead:string,startObjectId:string,endObjectId:string}):void ; addArrow(points: [[x:number,y:number]],formatting?:{startArrowHead:string,endArrowHead:string,startObjectId:string,endObjectId:string}):void ;
connectObjects(objectA: string, connectionA: ConnectionPoint, objectB: string, connectionB: ConnectionPoint, formatting?:{numberOfPoints: number,startArrowHead:string,endArrowHead:string, padding: number}):void; connectObjects(objectA: string, connectionA: ConnectionPoint, objectB: string, connectionB: ConnectionPoint, formatting?:{numberOfPoints: number,startArrowHead:string,endArrowHead:string, padding: number}):void;
@@ -269,8 +269,8 @@ export async function initExcalidrawAutomate(plugin: ExcalidrawPlugin) {
this.elementsDict[id] = boxedElement(id,"ellipse",topX,topY,width,height); this.elementsDict[id] = boxedElement(id,"ellipse",topX,topY,width,height);
return id; return id;
}, },
addText(topX:number, topY:number, text:string, formatting?:{width:number, height:number,textAlign: string, verticalAlign:string, box: boolean, boxPadding: number}):string { addText(topX:number, topY:number, text:string, formatting?:{width?:number, height?:number,textAlign?: string, verticalAlign?:string, box?: boolean, boxPadding?: number},id?:string):string {
const id = nanoid(); if(!id) id = nanoid();
const {w, h, baseline} = measureText(text, this.style.fontSize,this.style.fontFamily); const {w, h, baseline} = measureText(text, this.style.fontSize,this.style.fontFamily);
const width = formatting?.width ? formatting.width : w; const width = formatting?.width ? formatting.width : w;
const height = formatting?.height ? formatting.height : h; const height = formatting?.height ? formatting.height : h;

View File

@@ -399,18 +399,16 @@ export class ExcalidrawData {
return this.textElements.get(id)?.parsed; return this.textElements.get(id)?.parsed;
} }
public setTextElement(element:ExcalidrawTextElement, rawText:string, updateScene:Function):string { public setTextElement(elementID:string, rawText:string, updateScene:Function):string {
const parseResult = this.quickParse(rawText); //will return the parsed result if raw text does not include transclusion const parseResult = this.quickParse(rawText); //will return the parsed result if raw text does not include transclusion
if(parseResult) { //No transclusion if(parseResult) { //No transclusion
this.textElements.set(element.id,{raw: rawText,parsed: parseResult}); this.textElements.set(elementID,{raw: rawText,parsed: parseResult});
return parseResult; return parseResult;
} }
//transclusion needs to be resolved asynchornously //transclusion needs to be resolved asynchornously
this.parse(rawText).then((parsedText:string)=> { this.parse(rawText).then((parsedText:string)=> {
this.textElements.set(element.id,{raw: rawText,parsed: parsedText}); this.textElements.set(elementID,{raw: rawText,parsed: parsedText});
if(parsedText && element.text!=rawText) { if(parsedText) updateScene(parsedText);
updateScene();
}
}); });
return null; return null;
} }

View File

@@ -30,13 +30,14 @@ import {
TEXT_DISPLAY_PARSED_ICON_NAME, TEXT_DISPLAY_PARSED_ICON_NAME,
EXIT_FULLSCREEN_ICON_NAME, EXIT_FULLSCREEN_ICON_NAME,
FULLSCREEN_ICON_NAME, FULLSCREEN_ICON_NAME,
JSON_parse JSON_parse,
nanoid
} from './constants'; } from './constants';
import ExcalidrawPlugin from './main'; import ExcalidrawPlugin from './main';
import {ExcalidrawAutomate} from './ExcalidrawAutomate'; import {ExcalidrawAutomate} from './ExcalidrawAutomate';
import { t } from "./lang/helpers"; import { t } from "./lang/helpers";
import { ExcalidrawData, REG_LINK_BACKETS } from "./ExcalidrawData"; import { ExcalidrawData, REG_LINK_BACKETS } from "./ExcalidrawData";
import { checkAndCreateFolder, download, getIMGPathFromExcalidrawFile, getNewUniqueFilepath, randomInteger, splitFolderAndFilename } from "./Utils"; import { checkAndCreateFolder, download, getNewUniqueFilepath, splitFolderAndFilename } from "./Utils";
import { Prompt } from "./Prompt"; import { Prompt } from "./Prompt";
declare let window: ExcalidrawAutomate; declare let window: ExcalidrawAutomate;
@@ -248,7 +249,7 @@ export default class ExcalidrawView extends TextFileView {
onload() { onload() {
//console.log("ExcalidrawView.onload()"); //console.log("ExcalidrawView.onload()");
this.addAction(DISK_ICON_NAME,t("FORCE_SAVE"),async (ev)=> { this.addAction(DISK_ICON_NAME,t("FORCE_SAVE"),async (ev)=> {
await this.save(); await this.save(false);
this.plugin.triggerEmbedUpdates(); this.plugin.triggerEmbedUpdates();
}); });
@@ -291,7 +292,6 @@ export default class ExcalidrawView extends TextFileView {
} }
public async changeTextMode(textMode:TextMode,reload:boolean=true) { public async changeTextMode(textMode:TextMode,reload:boolean=true) {
//console.log("ExcalidrawView.lock(), locked",locked, "reload",reload);
this.textMode = textMode; this.textMode = textMode;
if(textMode == TextMode.parsed) { if(textMode == TextMode.parsed) {
this.textIsRaw_Element.hide(); this.textIsRaw_Element.hide();
@@ -309,7 +309,6 @@ export default class ExcalidrawView extends TextFileView {
public setupAutosaveTimer() { public setupAutosaveTimer() {
const timer = async () => { const timer = async () => {
if(this.dirty && (this.dirty == this.file?.path)) { if(this.dirty && (this.dirty == this.file?.path)) {
console.log("autosave",Date.now());
this.dirty = null; this.dirty = null;
this.autosaving=true; this.autosaving=true;
if(this.excalidrawRef) await this.save(); if(this.excalidrawRef) await this.save();
@@ -598,19 +597,33 @@ export default class ExcalidrawView extends TextFileView {
} }
const el: ExcalidrawElement[] = excalidrawRef.current.getSceneElements(); const el: ExcalidrawElement[] = excalidrawRef.current.getSceneElements();
const st: AppState = excalidrawRef.current.getAppState(); const st: AppState = excalidrawRef.current.getAppState();
const id = nanoid();
window.ExcalidrawAutomate.reset(); window.ExcalidrawAutomate.reset();
window.ExcalidrawAutomate.style.strokeColor = st.currentItemStrokeColor; window.ExcalidrawAutomate.style.strokeColor = st.currentItemStrokeColor;
window.ExcalidrawAutomate.style.opacity = st.currentItemOpacity; window.ExcalidrawAutomate.style.opacity = st.currentItemOpacity;
window.ExcalidrawAutomate.style.fontFamily = fontFamily ? fontFamily: st.currentItemFontFamily; window.ExcalidrawAutomate.style.fontFamily = fontFamily ? fontFamily: st.currentItemFontFamily;
window.ExcalidrawAutomate.style.fontSize = st.currentItemFontSize; window.ExcalidrawAutomate.style.fontSize = st.currentItemFontSize;
window.ExcalidrawAutomate.style.textAlign = st.currentItemTextAlign; window.ExcalidrawAutomate.style.textAlign = st.currentItemTextAlign;
const id = window.ExcalidrawAutomate.addText(currentPosition.x, currentPosition.y, text);
//@ts-ignore const addText = (text:string) => {
el.push(window.ExcalidrawAutomate.elementsDict[id]); window.ExcalidrawAutomate.addText(currentPosition.x, currentPosition.y, text,null,id);
excalidrawRef.current.updateScene({ //@ts-ignore
elements: el, const textElement = window.ExcalidrawAutomate.elementsDict[id];
appState: st, el.push(textElement);
excalidrawRef.current.updateScene({
elements: el,
appState: st,
});
this.save(false);
}
const self = this;
//setTextElement will attempt a quick parse (without processing transclusions)
const parseResult = this.excalidrawData.setTextElement(id, text,async (parsedText:string)=>{
addText(self.textMode==TextMode.parsed?parsedText:text);
}); });
if(parseResult) { //there were no transclusions in the raw text, quick parse was successful
addText(self.textMode==TextMode.parsed?parseResult:text);
}
} }
this.getScene = () => { this.getScene = () => {
@@ -666,7 +679,7 @@ export default class ExcalidrawView extends TextFileView {
if(!(this.plugin.settings.allowCtrlClick)) return; if(!(this.plugin.settings.allowCtrlClick)) return;
if(!this.getSelectedId()) return; if(!this.getSelectedId()) return;
this.handleLinkClick(this,e); this.handleLinkClick(this,e);
}, }
}, },
React.createElement(Excalidraw.default, { React.createElement(Excalidraw.default, {
ref: excalidrawRef, ref: excalidrawRef,
@@ -710,6 +723,10 @@ export default class ExcalidrawView extends TextFileView {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})(); })();
}, },
/*onPaste: (data: ClipboardData, event: ClipboardEvent | null) => {
console.log(data,event);
return true;
},*/
onBeforeTextEdit: (textElement: ExcalidrawTextElement) => { onBeforeTextEdit: (textElement: ExcalidrawTextElement) => {
if(this.autosaveTimer) { //stopping autosave to avoid autosave overwriting text while the user edits it if(this.autosaveTimer) { //stopping autosave to avoid autosave overwriting text while the user edits it
clearInterval(this.autosaveTimer); clearInterval(this.autosaveTimer);
@@ -729,7 +746,7 @@ export default class ExcalidrawView extends TextFileView {
//Then I need to clear the undo history to avoid overwriting raw text with parsed text and losing links //Then I need to clear the undo history to avoid overwriting raw text with parsed text and losing links
if(text!=textElement.text) { //the user made changes to the text if(text!=textElement.text) { //the user made changes to the text
//setTextElement will attempt a quick parse (without processing transclusions) //setTextElement will attempt a quick parse (without processing transclusions)
const parseResult = this.excalidrawData.setTextElement(textElement, text,async ()=>{ const parseResult = this.excalidrawData.setTextElement(textElement.id, text,async ()=>{
await this.save(false); await this.save(false);
//this callback function will only be invoked if quick parse fails, i.e. there is a transclusion in the raw text //this callback function will only be invoked if quick parse fails, i.e. there is a transclusion in the raw text
//thus I only check if TextMode.parsed, text is always != with parseResult //thus I only check if TextMode.parsed, text is always != with parseResult

View File

@@ -1,3 +1,3 @@
{ {
"1.2.10": "0.11.13" "1.2.11": "0.11.13"
} }