Skip to content

Commit 048e0a0

Browse files
authored
Feature/autosave (#36)
* Implemented autosave * Package update * Updated node versions in pipeline * Only support node 20 for build for now
1 parent d33d091 commit 048e0a0

File tree

9 files changed

+69
-12
lines changed

9 files changed

+69
-12
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [16.x, 18.x]
19+
node-version: [16.x, 18.x, 20.x]
2020
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2121

2222
steps:

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.0.0
2+
About time to make this package a 1.0.0 version!
3+
4+
- Added `autosave` property, which will trigger `onAutoSave` after every change
5+
16
# 0.2.0
27
- Added `csv` parameter for CSV loading support
38

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ function App() {
103103
All options are based on the documentation at <a href="https://www.drawio.com/doc/faq/embed-mode">draw.io/doc/faq/embed-mode</a>. If something is off, please let me know by creating an <a href="https://github.com/marcveens/react-drawio/issues/new">issue</a>.
104104

105105
### `props`
106+
- `autosave` (`boolean`, default: `false`)\
107+
When enabled, it will call `onAutoSave` for all changes made
106108
- `urlParameters` (`UrlParameters`, default: `undefined`)\
107109
Parameters documented at https://www.drawio.com/doc/faq/embed-mode
108110
- `xml` (`string`, default: `undefined`)\
@@ -117,6 +119,8 @@ All options are based on the documentation at <a href="https://www.drawio.com/do
117119
For self hosted instances of draw.io, insert your URL here
118120

119121
- `onLoad` (`(data: EventLoad) => void`, optional)
122+
- `onAutoSave` (`(data: EventAutoSave) => void`, optional)\
123+
This will only trigger when the `autosave` property is `true`
120124
- `onSave` (`(data: EventSave) => void`, optional)
121125
- `onClose` (`(data: EventExit) => void`, optional)
122126
- `onConfigure` (`(data: EventConfigure) => void`, optional)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-drawio",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"type": "module",
55
"description": "React component for integrating the Diagrams (draw.io) embed iframe",
66
"main": "index.js",

src/DrawIoEmbed.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import {
55
useRef,
66
useState
77
} from 'react';
8-
import { DrawIoEmbedProps, DrawIoEmbedRef } from './types';
8+
import { ActionLoad, DrawIoEmbedProps, DrawIoEmbedRef } from './types';
99
import { getEmbedUrl } from './utils/getEmbedUrl';
1010
import { handleEvent } from './utils/handleEvent';
11-
import { useActions } from './hooks/useActions';
11+
import { UniqueActionProps, useActions } from './hooks/useActions';
1212
import React from 'react';
1313

1414
export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
1515
(props, ref) => {
1616
const {
17+
autosave = false,
1718
baseUrl,
1819
urlParameters,
1920
configuration,
2021
xml,
2122
csv,
2223
exportFormat,
24+
onAutoSave,
2325
onSave,
2426
onClose,
2527
onLoad,
@@ -57,6 +59,11 @@ export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
5759
onConfigure(data);
5860
}
5961
},
62+
autosave: (data) => {
63+
if (onAutoSave) {
64+
onAutoSave(data);
65+
}
66+
},
6067
save: (data) => {
6168
action.exportDiagram({
6269
format: exportFormat || 'xmlsvg',
@@ -126,20 +133,29 @@ export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
126133
);
127134

128135
useEffect(() => {
136+
let loadObject: UniqueActionProps<ActionLoad> = {};
137+
129138
if (isInitialized) {
130139
if (xml) {
131140
if (exportFormat === 'xmlpng') {
132-
action.load({ xmlpng: xml });
141+
loadObject = { xmlpng: xml };
133142
} else {
134-
action.load({ xml });
143+
loadObject = { xml };
135144
}
136145
} else if (csv) {
137-
action.load({ descriptor: { format: 'csv', data: csv } });
146+
loadObject = { descriptor: { format: 'csv', data: csv } };
138147
} else {
139-
action.load({ xml: '' });
148+
loadObject = { xml: '' };
140149
}
150+
151+
loadObject = {
152+
...loadObject,
153+
autosave: autosave
154+
};
155+
156+
action.load(loadObject);
141157
}
142-
}, [isInitialized, xml, csv]);
158+
}, [isInitialized, xml, csv, autosave]);
143159

144160
// Initial load
145161
useEffect(() => {

src/hooks/useActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
EmbedActions
1515
} from '../types';
1616

17-
type UniqueActionProps<T> = Omit<T, 'action'>;
17+
export type UniqueActionProps<T> = Omit<T, 'action'>;
1818

1919
export const useActions = (iframeRef: RefObject<HTMLIFrameElement | null>) => {
2020
const sendAction = (

src/types.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { useActions } from './hooks/useActions';
22

33
export type DrawIoEmbedProps = {
4+
/**
5+
* This will cause the `onAutoSave` event to be triggered every time the diagram is changed
6+
*/
7+
autosave?: boolean;
48
/**
59
* Base URL for draw.io embed URL. Defaults to https://embed.diagrams.net
610
*/
@@ -23,6 +27,7 @@ export type DrawIoEmbedProps = {
2327
configuration?: { [key: string]: any };
2428
exportFormat?: ExportFormats;
2529
onLoad?: (data: EventLoad) => void;
30+
onAutoSave?: (data: EventAutoSave) => void;
2631
onSave?: (data: EventSave) => void;
2732
onClose?: (data: EventExit) => void;
2833
onConfigure?: (data: EventConfigure) => void;
@@ -93,6 +98,7 @@ type ExportFormats = 'html' | 'html2' | 'svg' | 'xmlsvg' | 'png' | 'xmlpng';
9398
export type EmbedEvents =
9499
| EventInit
95100
| EventLoad
101+
| EventAutoSave
96102
| EventSave
97103
| EventExit
98104
| EventConfigure
@@ -120,6 +126,17 @@ export type EventSave = {
120126
parentEvent?: string;
121127
};
122128

129+
export type EventAutoSave = {
130+
event: 'autosave';
131+
bounds: PagePosition;
132+
currentPage: number;
133+
page: PagePosition;
134+
pageVisible: boolean;
135+
scale: number;
136+
translate: { x: number; y: number };
137+
xml: string;
138+
};
139+
123140
export type EventExit = {
124141
event: 'exit';
125142
modified: boolean;
@@ -182,6 +199,7 @@ export type ActionLoad = {
182199
xml?: string;
183200
xmlpng?: string;
184201
descriptor?: { format: 'csv'; data: string };
202+
autosave?: boolean;
185203
};
186204

187205
export type ActionMerge = {
@@ -273,3 +291,10 @@ export type ActionExport = {
273291
/** Specifies the background color */
274292
background?: string;
275293
};
294+
295+
type PagePosition = {
296+
x: number;
297+
y: number;
298+
width: number;
299+
height: number;
300+
};

stories/DiagramsEmbed.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const meta: Meta<typeof DrawIoEmbed> = {
2121
},
2222
argTypes: {
2323
onLoad: { action: 'onLoad' },
24+
onAutoSave: { action: 'onAutoSave' },
2425
onSave: { action: 'onSave' },
2526
onClose: { action: 'onClose' },
2627
onConfigure: { action: 'onConfigure' },
@@ -365,6 +366,12 @@ export const ExportDataPng: Story = {
365366
]
366367
};
367368

369+
export const AutoSave: Story = {
370+
args: {
371+
autosave: true
372+
}
373+
};
374+
368375
export const NoSaveAndExit: Story = {
369376
args: {
370377
urlParameters: {

0 commit comments

Comments
 (0)