django-snapnote 0.6.0__tar.gz → 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/CHANGELOG.md +6 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/PKG-INFO +2 -2
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/README.md +1 -1
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/e2e/smoke.mjs +17 -3
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/App.tsx +18 -5
- django_snapnote-0.7.0/frontend/src/CropEditor.tsx +222 -0
- django_snapnote-0.7.0/frontend/src/crop.test.ts +66 -0
- django_snapnote-0.7.0/frontend/src/crop.ts +106 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/styles.css +1 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/types.ts +5 -0
- django_snapnote-0.7.0/src/snapnote/__init__.py +1 -0
- django_snapnote-0.7.0/src/snapnote/locale/es/LC_MESSAGES/django.mo +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/locale/es/LC_MESSAGES/django.po +15 -0
- django_snapnote-0.7.0/src/snapnote/static/snapnote/snapnote.js +40 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templatetags/snapnote_tags.py +5 -0
- django_snapnote-0.6.0/src/snapnote/__init__.py +0 -1
- django_snapnote-0.6.0/src/snapnote/locale/es/LC_MESSAGES/django.mo +0 -0
- django_snapnote-0.6.0/src/snapnote/static/snapnote/snapnote.js +0 -40
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/.gitignore +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/LICENSE +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/package-lock.json +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/package.json +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/AnnotationEditor.tsx +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/api.test.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/api.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/capture.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/drawing.test.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/drawing.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/icons.tsx +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/main.tsx +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/src/vite-env.d.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/tsconfig.json +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/frontend/vite.config.ts +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/pyproject.toml +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/admin.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/apps.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/checks.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/claude/ticket.md +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/conf.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/forms.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/jira.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/snapnote_export.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/snapnote_install_claude_command.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/snapnote_jira_export.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/snapnote_purge.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/management/commands/snapnote_retry_jira.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/middleware.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/migrations/0001_initial.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/migrations/0002_ticket_jira_key.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/migrations/0003_ticket_jira_error.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/migrations/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/models.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/notifiers.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/permissions.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/signals.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/static/snapnote/management.css +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/base.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/button.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/includes/status_badge.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/ticket_confirm_delete.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/ticket_detail.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templates/snapnote/ticket_list.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/templatetags/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/urls.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/views.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/conftest.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/helpers.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_button.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_claude_command.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_export.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_jira.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_jira_export.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_management.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/test_report.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/hostapp/__init__.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/hostapp/apps.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/hostapp/models.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/settings.py +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/templates/alt_base.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/templates/host_base.html +0 -0
- {django_snapnote-0.6.0 → django_snapnote-0.7.0}/tests/testproject/urls.py +0 -0
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
- The widget lets users crop the screenshot (also an attached image) before annotating it. Only the chosen area
|
|
6
|
+
is kept and sent; the full image is discarded in the browser. They can also skip the step.
|
|
7
|
+
- New labels (`crop`, `cropHint`, `applyCrop`, `resetCrop`, `skipCrop`) with Spanish translations.
|
|
8
|
+
|
|
3
9
|
## 0.6.0
|
|
4
10
|
|
|
5
11
|
- `snapnote_jira_export`: reads a Jira issue (read only, optional separate read credentials) into the same folder
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: django-snapnote
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Report problems from any page of a Django site: screenshot, annotations and a ticket list.
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -21,7 +21,7 @@ Description-Content-Type: text/markdown
|
|
|
21
21
|
# django-snapnote
|
|
22
22
|
|
|
23
23
|
Let your users report a problem from any page: a floating button captures the tab, lets them
|
|
24
|
-
draw on it and creates a ticket you can review in the admin or in built-in views.
|
|
24
|
+
crop it, draw on it and creates a ticket you can review in the admin or in built-in views.
|
|
25
25
|
|
|
26
26
|
Requires only Django (4.2–5.2) and Pillow. No DRF, no Node, no build step for you. The widget is a
|
|
27
27
|
self-contained bundle rendered in a Shadow DOM, so your CSS and ours don't interfere.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# django-snapnote
|
|
2
2
|
|
|
3
3
|
Let your users report a problem from any page: a floating button captures the tab, lets them
|
|
4
|
-
draw on it and creates a ticket you can review in the admin or in built-in views.
|
|
4
|
+
crop it, draw on it and creates a ticket you can review in the admin or in built-in views.
|
|
5
5
|
|
|
6
6
|
Requires only Django (4.2–5.2) and Pillow. No DRF, no Node, no build step for you. The widget is a
|
|
7
7
|
self-contained bundle rendered in a Shadow DOM, so your CSS and ours don't interfere.
|
|
@@ -64,9 +64,20 @@ async function scenario(browser, { capture, httpOnly = false }) {
|
|
|
64
64
|
|
|
65
65
|
const title = `E2E ${label}`;
|
|
66
66
|
if (capture) {
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
const crop = page.locator('.snapnote-crop-canvas');
|
|
68
|
+
await crop.waitFor();
|
|
69
|
+
check(`${label}: crop step shown first`, (await crop.count()) === 1);
|
|
70
|
+
const cropBox = await crop.boundingBox();
|
|
71
|
+
// Drag from the middle of the image (outside nothing: the full image is selected, so
|
|
72
|
+
// grab the top-left handle and pull it inwards to keep roughly the bottom-right half).
|
|
73
|
+
await page.mouse.move(cropBox.x + 1, cropBox.y + 1);
|
|
74
|
+
await page.mouse.down();
|
|
75
|
+
await page.mouse.move(cropBox.x + cropBox.width / 2, cropBox.y + cropBox.height / 2, { steps: 5 });
|
|
76
|
+
await page.mouse.up();
|
|
77
|
+
await page.getByRole('button', { name: 'Crop', exact: true }).click();
|
|
78
|
+
const canvas = page.locator('.snapnote-editor-canvas:not(.snapnote-crop-canvas)');
|
|
69
79
|
await canvas.waitFor();
|
|
80
|
+
check(`${label}: annotation canvas has the cropped size`, (await canvas.evaluate((c) => c.width)) < 640);
|
|
70
81
|
const box = await canvas.boundingBox();
|
|
71
82
|
await page.mouse.move(box.x + 100, box.y + 100);
|
|
72
83
|
await page.mouse.down();
|
|
@@ -82,7 +93,10 @@ async function scenario(browser, { capture, httpOnly = false }) {
|
|
|
82
93
|
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8z8BQz0AEYBxVSF+FABJADveWkH6oAAAAAElFTkSuQmCC', 'base64');
|
|
83
94
|
const file = join(dir, 'm.png'); writeFileSync(file, png);
|
|
84
95
|
await page.locator('input[type=file]').setInputFiles(file);
|
|
85
|
-
await page.locator('.snapnote-
|
|
96
|
+
await page.locator('.snapnote-crop-canvas').waitFor();
|
|
97
|
+
check(`${label}: uploaded image also goes through crop`, true);
|
|
98
|
+
await page.getByRole('button', { name: "Don't crop" }).click();
|
|
99
|
+
await page.locator('.snapnote-editor-canvas:not(.snapnote-crop-canvas)').waitFor();
|
|
86
100
|
await page.getByRole('button', { name: 'Done' }).click();
|
|
87
101
|
check(`${label}: uploaded image reaches the preview`, (await page.locator('.snapnote-preview img').count()) >= 1);
|
|
88
102
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React, { useEffect, useState } from 'react';
|
|
2
2
|
import { AnnotationEditor } from './AnnotationEditor';
|
|
3
|
+
import { CropEditor } from './CropEditor';
|
|
3
4
|
import { sendReport } from './api';
|
|
4
5
|
import { captureScreen } from './capture';
|
|
5
6
|
import { FlagIcon } from './icons';
|
|
6
7
|
import { Props, Shape } from './types';
|
|
7
8
|
|
|
8
|
-
type Phase = 'idle' | 'capturing' | 'editing' | 'form';
|
|
9
|
+
type Phase = 'idle' | 'capturing' | 'cropping' | 'editing' | 'form';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Floating button that captures the tab (or lets the user attach an image when
|
|
@@ -63,7 +64,7 @@ export function App({ report_url: reportUrl, csrf_token: csrfToken, labels, max_
|
|
|
63
64
|
setCaptureFailed(!blob);
|
|
64
65
|
setShapes([]);
|
|
65
66
|
setLayer(null);
|
|
66
|
-
setPhase(blob ? '
|
|
67
|
+
setPhase(blob ? 'cropping' : 'form');
|
|
67
68
|
if (!blob) setMessage({ ok: false, text: labels.noCapture });
|
|
68
69
|
};
|
|
69
70
|
|
|
@@ -77,7 +78,7 @@ export function App({ report_url: reportUrl, csrf_token: csrfToken, labels, max_
|
|
|
77
78
|
setScreenshot(file);
|
|
78
79
|
setShapes([]);
|
|
79
80
|
setLayer(null);
|
|
80
|
-
setPhase('
|
|
81
|
+
setPhase('cropping');
|
|
81
82
|
};
|
|
82
83
|
|
|
83
84
|
const submit = async (event: React.FormEvent) => {
|
|
@@ -102,7 +103,7 @@ export function App({ report_url: reportUrl, csrf_token: csrfToken, labels, max_
|
|
|
102
103
|
}
|
|
103
104
|
};
|
|
104
105
|
|
|
105
|
-
const reporting = phase === 'editing' || phase === 'form';
|
|
106
|
+
const reporting = phase === 'cropping' || phase === 'editing' || phase === 'form';
|
|
106
107
|
|
|
107
108
|
return (
|
|
108
109
|
<>
|
|
@@ -127,7 +128,7 @@ export function App({ report_url: reportUrl, csrf_token: csrfToken, labels, max_
|
|
|
127
128
|
{reporting && (
|
|
128
129
|
<div
|
|
129
130
|
className="snapnote-backdrop"
|
|
130
|
-
hidden={phase === 'editing'}
|
|
131
|
+
hidden={phase === 'cropping' || phase === 'editing'}
|
|
131
132
|
role="dialog"
|
|
132
133
|
aria-modal="true"
|
|
133
134
|
aria-label={labels.report}
|
|
@@ -206,6 +207,18 @@ export function App({ report_url: reportUrl, csrf_token: csrfToken, labels, max_
|
|
|
206
207
|
</div>
|
|
207
208
|
)}
|
|
208
209
|
|
|
210
|
+
{phase === 'cropping' && screenshot && (
|
|
211
|
+
<CropEditor
|
|
212
|
+
screenshot={screenshot}
|
|
213
|
+
labels={labels}
|
|
214
|
+
onDone={(cropped) => {
|
|
215
|
+
if (cropped) setScreenshot(cropped);
|
|
216
|
+
setPhase('editing');
|
|
217
|
+
}}
|
|
218
|
+
onCancel={() => setPhase('form')}
|
|
219
|
+
/>
|
|
220
|
+
)}
|
|
221
|
+
|
|
209
222
|
{phase === 'editing' && screenshot && (
|
|
210
223
|
<AnnotationEditor
|
|
211
224
|
screenshot={screenshot}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Handle,
|
|
4
|
+
Rect,
|
|
5
|
+
cropBlob,
|
|
6
|
+
fullRect,
|
|
7
|
+
hitHandle,
|
|
8
|
+
isFullRect,
|
|
9
|
+
rectFromPoints,
|
|
10
|
+
resizeRect,
|
|
11
|
+
} from './crop';
|
|
12
|
+
import { STROKE_PX, strokeWidthFor, toImagePoint } from './drawing';
|
|
13
|
+
import { Labels, Point } from './types';
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
screenshot: Blob;
|
|
17
|
+
labels: Labels;
|
|
18
|
+
/** The cropped image, or null to keep the screenshot as it is. */
|
|
19
|
+
onDone: (cropped: Blob | null) => void;
|
|
20
|
+
onCancel: () => void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Drag = { handle: Handle | 'new'; origin: Point; start: Rect };
|
|
24
|
+
|
|
25
|
+
const HANDLE_PX = 12;
|
|
26
|
+
const HIT_PX = 14;
|
|
27
|
+
|
|
28
|
+
const CURSORS: Record<Handle, string> = {
|
|
29
|
+
n: 'ns-resize',
|
|
30
|
+
s: 'ns-resize',
|
|
31
|
+
e: 'ew-resize',
|
|
32
|
+
w: 'ew-resize',
|
|
33
|
+
ne: 'nesw-resize',
|
|
34
|
+
sw: 'nesw-resize',
|
|
35
|
+
nw: 'nwse-resize',
|
|
36
|
+
se: 'nwse-resize',
|
|
37
|
+
move: 'move',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Size of one on-screen pixel, in screenshot pixels. */
|
|
41
|
+
const screenScale = (canvas: HTMLCanvasElement) => strokeWidthFor(canvas) / STROKE_PX;
|
|
42
|
+
|
|
43
|
+
function drawCrop(ctx: CanvasRenderingContext2D, rect: Rect, scale: number) {
|
|
44
|
+
const { canvas } = ctx;
|
|
45
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
46
|
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.55)';
|
|
47
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
48
|
+
ctx.clearRect(rect.x, rect.y, rect.width, rect.height);
|
|
49
|
+
ctx.lineWidth = 2 * scale;
|
|
50
|
+
ctx.strokeStyle = '#ffffff';
|
|
51
|
+
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
|
52
|
+
|
|
53
|
+
const size = HANDLE_PX * scale;
|
|
54
|
+
const xs = [rect.x, rect.x + rect.width / 2, rect.x + rect.width];
|
|
55
|
+
const ys = [rect.y, rect.y + rect.height / 2, rect.y + rect.height];
|
|
56
|
+
ctx.fillStyle = '#ffffff';
|
|
57
|
+
ctx.strokeStyle = '#1b1b1b';
|
|
58
|
+
ctx.lineWidth = scale;
|
|
59
|
+
xs.forEach((x, column) => {
|
|
60
|
+
ys.forEach((y, row) => {
|
|
61
|
+
if (column === 1 && row === 1) return;
|
|
62
|
+
ctx.fillRect(x - size / 2, y - size / 2, size, size);
|
|
63
|
+
ctx.strokeRect(x - size / 2, y - size / 2, size, size);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Lets the user keep only part of the screenshot before annotating it. The returned
|
|
70
|
+
* image really contains only the chosen area; nothing else is kept.
|
|
71
|
+
*/
|
|
72
|
+
export function CropEditor({ screenshot, labels, onDone, onCancel }: Props) {
|
|
73
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
74
|
+
const dragRef = useRef<Drag | null>(null);
|
|
75
|
+
|
|
76
|
+
const [imageUrl, setImageUrl] = useState('');
|
|
77
|
+
const [size, setSize] = useState({ width: 0, height: 0 });
|
|
78
|
+
const [rect, setRect] = useState<Rect>(fullRect(0, 0));
|
|
79
|
+
const [cursor, setCursor] = useState('crosshair');
|
|
80
|
+
const [busy, setBusy] = useState(false);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const url = URL.createObjectURL(screenshot);
|
|
84
|
+
setImageUrl(url);
|
|
85
|
+
return () => URL.revokeObjectURL(url);
|
|
86
|
+
}, [screenshot]);
|
|
87
|
+
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const canvas = canvasRef.current;
|
|
90
|
+
const ctx = canvas?.getContext('2d');
|
|
91
|
+
if (!canvas || !ctx || size.width === 0) return;
|
|
92
|
+
drawCrop(ctx, rect, screenScale(canvas));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
97
|
+
if (event.key === 'Escape') onCancel();
|
|
98
|
+
};
|
|
99
|
+
window.addEventListener('keydown', onKeyDown);
|
|
100
|
+
return () => window.removeEventListener('keydown', onKeyDown);
|
|
101
|
+
}, [onCancel]);
|
|
102
|
+
|
|
103
|
+
const pointOf = (event: React.PointerEvent<HTMLCanvasElement>) =>
|
|
104
|
+
toImagePoint(event.currentTarget, event.clientX, event.clientY);
|
|
105
|
+
|
|
106
|
+
const onPointerDown = (event: React.PointerEvent<HTMLCanvasElement>) => {
|
|
107
|
+
const canvas = event.currentTarget;
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
const point = pointOf(event);
|
|
110
|
+
const handle = hitHandle(rect, point, HIT_PX * screenScale(canvas));
|
|
111
|
+
canvas.setPointerCapture(event.pointerId);
|
|
112
|
+
dragRef.current = { handle: handle ?? 'new', origin: point, start: rect };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const onPointerMove = (event: React.PointerEvent<HTMLCanvasElement>) => {
|
|
116
|
+
const canvas = event.currentTarget;
|
|
117
|
+
const point = pointOf(event);
|
|
118
|
+
const drag = dragRef.current;
|
|
119
|
+
if (!drag) {
|
|
120
|
+
const handle = hitHandle(rect, point, HIT_PX * screenScale(canvas));
|
|
121
|
+
setCursor(handle ? CURSORS[handle] : 'crosshair');
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (drag.handle === 'new') {
|
|
125
|
+
setRect(rectFromPoints(drag.origin, point, size.width, size.height));
|
|
126
|
+
} else {
|
|
127
|
+
setRect(
|
|
128
|
+
resizeRect(
|
|
129
|
+
drag.start,
|
|
130
|
+
drag.handle,
|
|
131
|
+
point.x - drag.origin.x,
|
|
132
|
+
point.y - drag.origin.y,
|
|
133
|
+
size.width,
|
|
134
|
+
size.height,
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const onPointerUp = () => {
|
|
141
|
+
const drag = dragRef.current;
|
|
142
|
+
dragRef.current = null;
|
|
143
|
+
// A click or a tiny drag must not leave an unusable selection behind.
|
|
144
|
+
if (drag && drag.handle === 'new' && (rect.width < 16 || rect.height < 16)) {
|
|
145
|
+
setRect(drag.start);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const apply = async () => {
|
|
150
|
+
if (isFullRect(rect, size.width, size.height)) {
|
|
151
|
+
onDone(null);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
setBusy(true);
|
|
155
|
+
try {
|
|
156
|
+
onDone(await cropBlob(screenshot, rect));
|
|
157
|
+
} catch {
|
|
158
|
+
onDone(null);
|
|
159
|
+
} finally {
|
|
160
|
+
setBusy(false);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div className="snapnote-editor" role="dialog" aria-modal="true" aria-label={labels.crop}>
|
|
166
|
+
<div className="snapnote-editor-toolbar">
|
|
167
|
+
<span className="snapnote-crop-hint">{labels.cropHint}</span>
|
|
168
|
+
<span className="snapnote-spacer" />
|
|
169
|
+
<button
|
|
170
|
+
type="button"
|
|
171
|
+
className="snapnote-btn"
|
|
172
|
+
disabled={isFullRect(rect, size.width, size.height)}
|
|
173
|
+
onClick={() => setRect(fullRect(size.width, size.height))}
|
|
174
|
+
>
|
|
175
|
+
{labels.resetCrop}
|
|
176
|
+
</button>
|
|
177
|
+
<button type="button" className="snapnote-btn" onClick={onCancel}>
|
|
178
|
+
{labels.cancel} (Esc)
|
|
179
|
+
</button>
|
|
180
|
+
<button type="button" className="snapnote-btn" onClick={() => onDone(null)}>
|
|
181
|
+
{labels.skipCrop}
|
|
182
|
+
</button>
|
|
183
|
+
<button
|
|
184
|
+
type="button"
|
|
185
|
+
className="snapnote-btn snapnote-btn-primary"
|
|
186
|
+
disabled={busy || size.width === 0}
|
|
187
|
+
onClick={apply}
|
|
188
|
+
>
|
|
189
|
+
{labels.applyCrop}
|
|
190
|
+
</button>
|
|
191
|
+
</div>
|
|
192
|
+
<div className="snapnote-editor-stage">
|
|
193
|
+
{imageUrl && (
|
|
194
|
+
<img
|
|
195
|
+
className="snapnote-editor-image"
|
|
196
|
+
src={imageUrl}
|
|
197
|
+
alt=""
|
|
198
|
+
draggable={false}
|
|
199
|
+
onLoad={(event) => {
|
|
200
|
+
const img = event.currentTarget;
|
|
201
|
+
setSize({ width: img.naturalWidth, height: img.naturalHeight });
|
|
202
|
+
setRect(fullRect(img.naturalWidth, img.naturalHeight));
|
|
203
|
+
}}
|
|
204
|
+
/>
|
|
205
|
+
)}
|
|
206
|
+
{size.width > 0 && (
|
|
207
|
+
<canvas
|
|
208
|
+
ref={canvasRef}
|
|
209
|
+
className="snapnote-editor-canvas snapnote-crop-canvas"
|
|
210
|
+
// The cursor depends on what is under the pointer, so it can't be in the stylesheet.
|
|
211
|
+
style={{ cursor }}
|
|
212
|
+
width={size.width}
|
|
213
|
+
height={size.height}
|
|
214
|
+
onPointerDown={onPointerDown}
|
|
215
|
+
onPointerMove={onPointerMove}
|
|
216
|
+
onPointerUp={onPointerUp}
|
|
217
|
+
/>
|
|
218
|
+
)}
|
|
219
|
+
</div>
|
|
220
|
+
</div>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
MIN_CROP,
|
|
4
|
+
fullRect,
|
|
5
|
+
hitHandle,
|
|
6
|
+
isFullRect,
|
|
7
|
+
rectFromPoints,
|
|
8
|
+
resizeRect,
|
|
9
|
+
} from './crop';
|
|
10
|
+
|
|
11
|
+
const W = 400;
|
|
12
|
+
const H = 200;
|
|
13
|
+
const start = { x: 100, y: 50, width: 200, height: 100 };
|
|
14
|
+
|
|
15
|
+
describe('rectFromPoints', () => {
|
|
16
|
+
it('orders the corners and clamps to the image', () => {
|
|
17
|
+
expect(rectFromPoints({ x: 300, y: 150 }, { x: 100, y: 50 }, W, H)).toEqual(start);
|
|
18
|
+
expect(rectFromPoints({ x: -20, y: -5 }, { x: 500, y: 90 }, W, H)).toEqual({
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
width: W,
|
|
22
|
+
height: 90,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('resizeRect', () => {
|
|
28
|
+
it('moves without leaving the image', () => {
|
|
29
|
+
expect(resizeRect(start, 'move', 20, 10, W, H)).toMatchObject({ x: 120, y: 60 });
|
|
30
|
+
expect(resizeRect(start, 'move', 999, 999, W, H)).toMatchObject({ x: 200, y: 100 });
|
|
31
|
+
expect(resizeRect(start, 'move', -999, -999, W, H)).toMatchObject({ x: 0, y: 0 });
|
|
32
|
+
});
|
|
33
|
+
it('resizes from a corner', () => {
|
|
34
|
+
expect(resizeRect(start, 'se', 50, 25, W, H)).toEqual({ x: 100, y: 50, width: 250, height: 125 });
|
|
35
|
+
expect(resizeRect(start, 'nw', 10, 10, W, H)).toEqual({ x: 110, y: 60, width: 190, height: 90 });
|
|
36
|
+
});
|
|
37
|
+
it('resizes from an edge, leaving the other axis alone', () => {
|
|
38
|
+
expect(resizeRect(start, 'e', 30, 99, W, H)).toEqual({ x: 100, y: 50, width: 230, height: 100 });
|
|
39
|
+
expect(resizeRect(start, 'n', 99, -20, W, H)).toEqual({ x: 100, y: 30, width: 200, height: 120 });
|
|
40
|
+
});
|
|
41
|
+
it('keeps the minimum size and the image bounds', () => {
|
|
42
|
+
expect(resizeRect(start, 'w', 999, 0, W, H)).toMatchObject({ x: 300 - MIN_CROP, width: MIN_CROP });
|
|
43
|
+
expect(resizeRect(start, 's', 0, 999, W, H)).toMatchObject({ height: H - 50 });
|
|
44
|
+
expect(resizeRect(start, 'e', -999, 0, W, H)).toMatchObject({ x: 100, width: MIN_CROP });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('hitHandle', () => {
|
|
49
|
+
it('detects corners, edges and the inside', () => {
|
|
50
|
+
expect(hitHandle(start, { x: 100, y: 50 }, 5)).toBe('nw');
|
|
51
|
+
expect(hitHandle(start, { x: 300, y: 150 }, 5)).toBe('se');
|
|
52
|
+
expect(hitHandle(start, { x: 200, y: 52 }, 5)).toBe('n');
|
|
53
|
+
expect(hitHandle(start, { x: 298, y: 100 }, 5)).toBe('e');
|
|
54
|
+
expect(hitHandle(start, { x: 200, y: 100 }, 5)).toBe('move');
|
|
55
|
+
});
|
|
56
|
+
it('returns null outside', () => {
|
|
57
|
+
expect(hitHandle(start, { x: 10, y: 10 }, 5)).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('isFullRect', () => {
|
|
62
|
+
it('is true only for the whole image', () => {
|
|
63
|
+
expect(isFullRect(fullRect(W, H), W, H)).toBe(true);
|
|
64
|
+
expect(isFullRect(start, W, H)).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Point } from './types';
|
|
2
|
+
|
|
3
|
+
/** A rectangle in screenshot pixel coordinates. */
|
|
4
|
+
export type Rect = { x: number; y: number; width: number; height: number };
|
|
5
|
+
|
|
6
|
+
export type Handle = 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw' | 'move';
|
|
7
|
+
|
|
8
|
+
/** Smallest crop side, in screenshot pixels. */
|
|
9
|
+
export const MIN_CROP = 16;
|
|
10
|
+
|
|
11
|
+
const clamp = (value: number, min: number, max: number) =>
|
|
12
|
+
Math.min(Math.max(value, min), max);
|
|
13
|
+
|
|
14
|
+
export const fullRect = (width: number, height: number): Rect => ({
|
|
15
|
+
x: 0,
|
|
16
|
+
y: 0,
|
|
17
|
+
width,
|
|
18
|
+
height,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const isFullRect = (rect: Rect, width: number, height: number) =>
|
|
22
|
+
rect.x <= 0 && rect.y <= 0 && rect.width >= width && rect.height >= height;
|
|
23
|
+
|
|
24
|
+
/** Rectangle spanned by two points, kept inside the image. */
|
|
25
|
+
export function rectFromPoints(a: Point, b: Point, width: number, height: number): Rect {
|
|
26
|
+
const x1 = clamp(Math.min(a.x, b.x), 0, width);
|
|
27
|
+
const y1 = clamp(Math.min(a.y, b.y), 0, height);
|
|
28
|
+
const x2 = clamp(Math.max(a.x, b.x), 0, width);
|
|
29
|
+
const y2 = clamp(Math.max(a.y, b.y), 0, height);
|
|
30
|
+
return { x: x1, y: y1, width: x2 - x1, height: y2 - y1 };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Moves or resizes `start` by the total pointer movement (dx, dy), always inside the
|
|
35
|
+
* image and never smaller than MIN_CROP.
|
|
36
|
+
*/
|
|
37
|
+
export function resizeRect(
|
|
38
|
+
start: Rect,
|
|
39
|
+
handle: Handle,
|
|
40
|
+
dx: number,
|
|
41
|
+
dy: number,
|
|
42
|
+
width: number,
|
|
43
|
+
height: number,
|
|
44
|
+
): Rect {
|
|
45
|
+
if (handle === 'move') {
|
|
46
|
+
return {
|
|
47
|
+
x: clamp(start.x + dx, 0, width - start.width),
|
|
48
|
+
y: clamp(start.y + dy, 0, height - start.height),
|
|
49
|
+
width: start.width,
|
|
50
|
+
height: start.height,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
let left = start.x;
|
|
54
|
+
let top = start.y;
|
|
55
|
+
let right = start.x + start.width;
|
|
56
|
+
let bottom = start.y + start.height;
|
|
57
|
+
if (handle.includes('w')) left = clamp(left + dx, 0, right - MIN_CROP);
|
|
58
|
+
if (handle.includes('e')) right = clamp(right + dx, left + MIN_CROP, width);
|
|
59
|
+
if (handle.includes('n')) top = clamp(top + dy, 0, bottom - MIN_CROP);
|
|
60
|
+
if (handle.includes('s')) bottom = clamp(bottom + dy, top + MIN_CROP, height);
|
|
61
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Which part of the rectangle is under `point`: a corner or edge (within `tolerance`),
|
|
66
|
+
* its inside ('move') or null when the point is outside.
|
|
67
|
+
*/
|
|
68
|
+
export function hitHandle(rect: Rect, point: Point, tolerance: number): Handle | null {
|
|
69
|
+
const right = rect.x + rect.width;
|
|
70
|
+
const bottom = rect.y + rect.height;
|
|
71
|
+
const inside =
|
|
72
|
+
point.x >= rect.x - tolerance &&
|
|
73
|
+
point.x <= right + tolerance &&
|
|
74
|
+
point.y >= rect.y - tolerance &&
|
|
75
|
+
point.y <= bottom + tolerance;
|
|
76
|
+
if (!inside) return null;
|
|
77
|
+
let vertical = '';
|
|
78
|
+
if (Math.abs(point.y - rect.y) <= tolerance) vertical = 'n';
|
|
79
|
+
else if (Math.abs(point.y - bottom) <= tolerance) vertical = 's';
|
|
80
|
+
let horizontal = '';
|
|
81
|
+
if (Math.abs(point.x - rect.x) <= tolerance) horizontal = 'w';
|
|
82
|
+
else if (Math.abs(point.x - right) <= tolerance) horizontal = 'e';
|
|
83
|
+
return (vertical + horizontal || 'move') as Handle;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Cropped copy of the image as a PNG; null if it cannot be produced. */
|
|
87
|
+
export async function cropBlob(blob: Blob, rect: Rect): Promise<Blob | null> {
|
|
88
|
+
const bitmap = await createImageBitmap(blob);
|
|
89
|
+
try {
|
|
90
|
+
const x = clamp(Math.round(rect.x), 0, bitmap.width - 1);
|
|
91
|
+
const y = clamp(Math.round(rect.y), 0, bitmap.height - 1);
|
|
92
|
+
const width = clamp(Math.round(rect.width), 1, bitmap.width - x);
|
|
93
|
+
const height = clamp(Math.round(rect.height), 1, bitmap.height - y);
|
|
94
|
+
const canvas = document.createElement('canvas');
|
|
95
|
+
canvas.width = width;
|
|
96
|
+
canvas.height = height;
|
|
97
|
+
const ctx = canvas.getContext('2d');
|
|
98
|
+
if (!ctx) return null;
|
|
99
|
+
ctx.drawImage(bitmap, x, y, width, height, 0, 0, width, height);
|
|
100
|
+
return await new Promise<Blob | null>((resolve) => {
|
|
101
|
+
canvas.toBlob(resolve, 'image/png');
|
|
102
|
+
});
|
|
103
|
+
} finally {
|
|
104
|
+
bitmap.close();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -68,3 +68,4 @@
|
|
|
68
68
|
position: absolute; padding: 0 .25rem; font-family: sans-serif; font-weight: 700;
|
|
69
69
|
color: var(--snapnote-accent); background: rgba(255, 255, 255, .85); border: 1px dashed var(--snapnote-accent); outline: 0;
|
|
70
70
|
}
|
|
71
|
+
.snapnote-crop-hint { font-size: .9rem; color: #323f4b; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.0"
|
|
Binary file
|
{django_snapnote-0.6.0 → django_snapnote-0.7.0}/src/snapnote/locale/es/LC_MESSAGES/django.po
RENAMED
|
@@ -143,6 +143,21 @@ msgstr "Listo"
|
|
|
143
143
|
msgid "The title is required."
|
|
144
144
|
msgstr "El título es obligatorio."
|
|
145
145
|
|
|
146
|
+
msgid "Crop the screenshot"
|
|
147
|
+
msgstr "Recortar la captura"
|
|
148
|
+
|
|
149
|
+
msgid "Drag to choose the area to keep"
|
|
150
|
+
msgstr "Arrastra para elegir el área que quieres conservar"
|
|
151
|
+
|
|
152
|
+
msgid "Crop"
|
|
153
|
+
msgstr "Recortar"
|
|
154
|
+
|
|
155
|
+
msgid "Reset"
|
|
156
|
+
msgstr "Restablecer"
|
|
157
|
+
|
|
158
|
+
msgid "Don't crop"
|
|
159
|
+
msgstr "No recortar"
|
|
160
|
+
|
|
146
161
|
msgid "Tickets"
|
|
147
162
|
msgstr "Tickets"
|
|
148
163
|
|