11// Docs: https://www.diagrams.net/doc/faq/embed-mode
22import * as store from './store' ;
3-
4- let iFrame = null ;
5- let lastApprovedOrigin ;
6- let onInit ;
7- let onSave ;
3+ import { ConfirmDialog } from "../components" ;
4+
5+ type DrawioExportEventResponse = {
6+ action : 'export' ,
7+ format : string ,
8+ message : string ,
9+ data : string ,
10+ xml : string ,
11+ } ;
12+
13+ type DrawioSaveEventResponse = {
14+ action : 'save' ,
15+ xml : string ,
16+ } ;
17+
18+ let iFrame : HTMLIFrameElement | null = null ;
19+ let lastApprovedOrigin : string ;
20+ let onInit : ( ) => Promise < string > ;
21+ let onSave : ( data : string ) => Promise < any > ;
822const saveBackupKey = 'last-drawing-save' ;
923
10- function drawPostMessage ( data ) {
11- iFrame . contentWindow . postMessage ( JSON . stringify ( data ) , lastApprovedOrigin ) ;
24+ function drawPostMessage ( data : Record < any , any > ) : void {
25+ iFrame ? .contentWindow ? .postMessage ( JSON . stringify ( data ) , lastApprovedOrigin ) ;
1226}
1327
14- function drawEventExport ( message ) {
28+ function drawEventExport ( message : DrawioExportEventResponse ) {
1529 store . set ( saveBackupKey , message . data ) ;
1630 if ( onSave ) {
1731 onSave ( message . data ) . then ( ( ) => {
@@ -20,7 +34,7 @@ function drawEventExport(message) {
2034 }
2135}
2236
23- function drawEventSave ( message ) {
37+ function drawEventSave ( message : DrawioSaveEventResponse ) {
2438 drawPostMessage ( {
2539 action : 'export' , format : 'xmlpng' , xml : message . xml , spin : 'Updating drawing' ,
2640 } ) ;
@@ -35,8 +49,10 @@ function drawEventInit() {
3549
3650function drawEventConfigure ( ) {
3751 const config = { } ;
38- window . $events . emitPublic ( iFrame , 'editor-drawio::configure' , { config} ) ;
39- drawPostMessage ( { action : 'configure' , config} ) ;
52+ if ( iFrame ) {
53+ window . $events . emitPublic ( iFrame , 'editor-drawio::configure' , { config} ) ;
54+ drawPostMessage ( { action : 'configure' , config} ) ;
55+ }
4056}
4157
4258function drawEventClose ( ) {
@@ -47,9 +63,8 @@ function drawEventClose() {
4763
4864/**
4965 * Receive and handle a message event from the draw.io window.
50- * @param {MessageEvent } event
5166 */
52- function drawReceive ( event ) {
67+ function drawReceive ( event : MessageEvent ) {
5368 if ( ! event . data || event . data . length < 1 ) return ;
5469 if ( event . origin !== lastApprovedOrigin ) return ;
5570
@@ -59,9 +74,9 @@ function drawReceive(event) {
5974 } else if ( message . event === 'exit' ) {
6075 drawEventClose ( ) ;
6176 } else if ( message . event === 'save' ) {
62- drawEventSave ( message ) ;
77+ drawEventSave ( message as DrawioSaveEventResponse ) ;
6378 } else if ( message . event === 'export' ) {
64- drawEventExport ( message ) ;
79+ drawEventExport ( message as DrawioExportEventResponse ) ;
6580 } else if ( message . event === 'configure' ) {
6681 drawEventConfigure ( ) ;
6782 }
@@ -79,9 +94,8 @@ async function attemptRestoreIfExists() {
7994 console . error ( 'Missing expected unsaved-drawing dialog' ) ;
8095 }
8196
82- if ( backupVal ) {
83- /** @var {ConfirmDialog} */
84- const dialog = window . $components . firstOnElement ( dialogEl , 'confirm-dialog' ) ;
97+ if ( backupVal && dialogEl ) {
98+ const dialog = window . $components . firstOnElement ( dialogEl , 'confirm-dialog' ) as ConfirmDialog ;
8599 const restore = await dialog . show ( ) ;
86100 if ( restore ) {
87101 onInit = async ( ) => backupVal ;
@@ -94,11 +108,9 @@ async function attemptRestoreIfExists() {
94108 * onSaveCallback must return a promise that resolves on successful save and errors on failure.
95109 * onInitCallback must return a promise with the xml to load for the editor.
96110 * Will attempt to provide an option to restore unsaved changes if found to exist.
97- * @param {String } drawioUrl
98- * @param {Function<Promise<String>> } onInitCallback
99- * @param {Function<Promise> } onSaveCallback - Is called with the drawing data on save.
111+ * onSaveCallback Is called with the drawing data on save.
100112 */
101- export async function show ( drawioUrl , onInitCallback , onSaveCallback ) {
113+ export async function show ( drawioUrl : string , onInitCallback : ( ) => Promise < string > , onSaveCallback : ( data : string ) => Promise < void > ) : Promise < void > {
102114 onInit = onInitCallback ;
103115 onSave = onSaveCallback ;
104116
@@ -114,7 +126,7 @@ export async function show(drawioUrl, onInitCallback, onSaveCallback) {
114126 lastApprovedOrigin = ( new URL ( drawioUrl ) ) . origin ;
115127}
116128
117- export async function upload ( imageData , pageUploadedToId ) {
129+ export async function upload ( imageData : string , pageUploadedToId : string ) : Promise < { } | string > {
118130 const data = {
119131 image : imageData ,
120132 uploaded_to : pageUploadedToId ,
@@ -129,10 +141,8 @@ export function close() {
129141
130142/**
131143 * Load an existing image, by fetching it as Base64 from the system.
132- * @param drawingId
133- * @returns {Promise<string> }
134144 */
135- export async function load ( drawingId ) {
145+ export async function load ( drawingId : string ) : Promise < string > {
136146 try {
137147 const resp = await window . $http . get ( window . baseUrl ( `/images/drawio/base64/${ drawingId } ` ) ) ;
138148 return `data:image/png;base64,${ resp . data . content } ` ;
0 commit comments