Skip to content

Commit 14dcb56

Browse files
authored
Let unfinished doodles persist when not actively doodling (#10)
canvas sizing is still kind of an issue, we should look into that
1 parent ca50cba commit 14dcb56

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/annotator/guest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ export default class Guest extends Delegator {
775775
$doodle: true,
776776
doodleLines: this.doodleCanvasController.lines,
777777
});
778+
this.doodleCanvasController.lines = [];
778779
}
779780
}
780781
}

src/doodle/doodleCanvas.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createElement } from 'preact';
2-
import { useState } from 'preact/hooks';
2+
import { useState, useEffect } from 'preact/hooks';
33
import { Canvas } from './canvas';
44
import propTypes from 'prop-types';
55

@@ -31,6 +31,28 @@ const DoodleCanvas = ({
3131
setLines,
3232
}) => {
3333
const [isDrawing, setIsDrawing] = useState(false);
34+
const [everActive, setEverActive] = useState(false);
35+
36+
if (active && !everActive) {
37+
setEverActive(true);
38+
}
39+
40+
useEffect(() => {
41+
if (lines.length === 0) {
42+
return () => {};
43+
}
44+
const warn = e => {
45+
e = e || window.event;
46+
47+
e.preventDefault();
48+
e.returnValue = '';
49+
return '';
50+
};
51+
window.addEventListener('beforeunload', warn);
52+
return () => {
53+
window.removeEventListener('beforeunload', warn);
54+
};
55+
}, [lines]);
3456

3557
const handleMouseDown = e => {
3658
setIsDrawing(true);
@@ -74,7 +96,7 @@ const DoodleCanvas = ({
7496
setLines([newLine, ...rest]);
7597
};
7698

77-
if (!active) {
99+
if (!everActive) {
78100
return null;
79101
}
80102

0 commit comments

Comments
 (0)