nmrium 0.39.0 → 0.40.1
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.
- package/lib/component/context/LoggerContext.js +11 -6
- package/lib/component/elements/popup/Alert/AlertBlock.d.ts +7 -0
- package/lib/component/elements/popup/Alert/AlertBlock.js +24 -0
- package/lib/component/elements/popup/Alert/Provider.js +2 -20
- package/lib/component/elements/popup/Modal/ConfirmDialog.js +2 -1
- package/lib/component/header/Header.js +2 -2
- package/lib/component/modal/AboutUsModal.js +4 -2
- package/lib/component/modal/LogsHistoryModal.d.ts +6 -0
- package/lib/component/{header/LogsHistory.js → modal/LogsHistoryModal.js} +6 -4
- package/lib/component/modal/setting/GeneralSettings.js +28 -27
- package/lib/component/panels/MoleculesPanel/MoleculePanelHeader.js +18 -16
- package/lib/component/panels/MultipleSpectraAnalysisPanel/MultipleSpectraAnalysisPanel.js +9 -13
- package/lib/component/panels/RangesPanel/RangesHeader.js +9 -15
- package/lib/component/panels/RangesPanel/RangesPanel.js +7 -10
- package/lib/component/panels/SpectrumsPanel/SpectraTable.js +7 -10
- package/lib/component/utility/export.d.ts +5 -4
- package/lib/component/utility/export.js +24 -52
- package/lib/utils/clipboard/clipboard.d.ts +12 -0
- package/lib/utils/clipboard/clipboard.js +15 -0
- package/lib/utils/clipboard/clipboardComponents.d.ts +45 -0
- package/lib/utils/clipboard/clipboardComponents.js +150 -0
- package/lib/utils/clipboard/clipboardHooks.d.ts +94 -0
- package/lib/utils/clipboard/clipboardHooks.js +142 -0
- package/lib/utils/clipboard/types.d.ts +1 -0
- package/lib/utils/clipboard/types.js +1 -0
- package/lib/versionInfo.js +1 -1
- package/lib-cjs/component/context/LoggerContext.js +10 -5
- package/lib-cjs/component/elements/popup/Alert/AlertBlock.js +30 -0
- package/lib-cjs/component/elements/popup/Alert/Provider.js +2 -20
- package/lib-cjs/component/elements/popup/Modal/ConfirmDialog.js +2 -1
- package/lib-cjs/component/header/Header.js +2 -2
- package/lib-cjs/component/modal/AboutUsModal.js +4 -2
- package/lib-cjs/component/{header/LogsHistory.js → modal/LogsHistoryModal.js} +8 -6
- package/lib-cjs/component/modal/setting/GeneralSettings.js +28 -27
- package/lib-cjs/component/panels/MoleculesPanel/MoleculePanelHeader.js +17 -15
- package/lib-cjs/component/panels/MultipleSpectraAnalysisPanel/MultipleSpectraAnalysisPanel.js +9 -13
- package/lib-cjs/component/panels/RangesPanel/RangesHeader.js +9 -15
- package/lib-cjs/component/panels/RangesPanel/RangesPanel.js +7 -10
- package/lib-cjs/component/panels/SpectrumsPanel/SpectraTable.js +7 -10
- package/lib-cjs/component/utility/export.js +24 -54
- package/lib-cjs/utils/clipboard/clipboard.js +23 -0
- package/lib-cjs/utils/clipboard/clipboardComponents.js +158 -0
- package/lib-cjs/utils/clipboard/clipboardHooks.js +147 -0
- package/lib-cjs/utils/clipboard/types.js +2 -0
- package/lib-cjs/versionInfo.js +1 -1
- package/package.json +13 -13
- package/lib/component/header/LogsHistory.d.ts +0 -1
|
@@ -4,12 +4,13 @@ import { useMemo, useCallback, useState } from 'react';
|
|
|
4
4
|
import { FaCopy, FaRegTrashAlt, FaFileExport } from 'react-icons/fa';
|
|
5
5
|
import { IoColorPaletteOutline } from 'react-icons/io5';
|
|
6
6
|
import { DropdownMenu } from 'react-science/ui';
|
|
7
|
+
import { ClipboardFallbackModal } from '../../../utils/clipboard/clipboardComponents';
|
|
8
|
+
import { useClipboard } from '../../../utils/clipboard/clipboardHooks';
|
|
7
9
|
import { useDispatch } from '../../context/DispatchContext';
|
|
8
10
|
import ReactTable from '../../elements/ReactTable/ReactTable';
|
|
9
11
|
import { useAlert } from '../../elements/popup/Alert';
|
|
10
12
|
import { usePanelPreferences } from '../../hooks/usePanelPreferences';
|
|
11
13
|
import ExportAsJcampModal from '../../modal/ExportAsJcampModal';
|
|
12
|
-
import { copyTextToClipboard } from '../../utility/export';
|
|
13
14
|
import ColorIndicator from './base/ColorIndicator';
|
|
14
15
|
import ShowHideSpectrumButton from './base/ShowHideSpectrumButton';
|
|
15
16
|
import { SpectrumName } from './base/SpectrumName';
|
|
@@ -130,19 +131,15 @@ export function SpectraTable(props) {
|
|
|
130
131
|
},
|
|
131
132
|
},
|
|
132
133
|
}), [onChangeVisibility, onOpenSettingModal]);
|
|
134
|
+
const { rawWriteWithType, cleanShouldFallback, shouldFallback, text } = useClipboard();
|
|
133
135
|
const selectContextMenuHandler = useCallback((option, spectrum) => {
|
|
134
136
|
const { id } = option.data;
|
|
135
137
|
switch (id) {
|
|
136
138
|
case SpectraContextMenuOptionsKeys.CopyToClipboard: {
|
|
137
139
|
void (async () => {
|
|
138
140
|
const { data, info } = spectrum;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
alert.success('Data copied to clipboard');
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
alert.error('Copy to clipboard failed');
|
|
145
|
-
}
|
|
141
|
+
await rawWriteWithType(JSON.stringify({ data, info }, (_, value) => ArrayBuffer.isView(value) ? Array.from(value) : value, 2));
|
|
142
|
+
alert.success('Data copied to clipboard');
|
|
146
143
|
})();
|
|
147
144
|
break;
|
|
148
145
|
}
|
|
@@ -160,7 +157,7 @@ export function SpectraTable(props) {
|
|
|
160
157
|
break;
|
|
161
158
|
}
|
|
162
159
|
}
|
|
163
|
-
}, [alert, dispatch]);
|
|
160
|
+
}, [alert, dispatch, rawWriteWithType]);
|
|
164
161
|
function handleActiveRow(row) {
|
|
165
162
|
return activeSpectraObj?.[row?.original.id] || false;
|
|
166
163
|
}
|
|
@@ -214,7 +211,7 @@ export function SpectraTable(props) {
|
|
|
214
211
|
? Spectra1DContextMenuOptions
|
|
215
212
|
: Spectra2DContextMenuOptions, onContextMenuSelect: selectContextMenuHandler, onSortEnd: handleSortEnd, style: { 'table td': { paddingTop: 0, paddingBottom: 0 } } }), exportedSpectrum && (_jsx(ExportAsJcampModal, { spectrum: exportedSpectrum, closeDialog: () => {
|
|
216
213
|
setExportedSpectrum(null);
|
|
217
|
-
} }))] }));
|
|
214
|
+
} })), _jsx(ClipboardFallbackModal, { mode: shouldFallback, onDismiss: cleanShouldFallback, text: text, label: "Spectra" })] }));
|
|
218
215
|
}
|
|
219
216
|
const ColumnHeader = ({ label, col, }) => {
|
|
220
217
|
const dispatch = useDispatch();
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
declare function copyHTMLToClipboard(data: any): Promise<boolean>;
|
|
2
|
-
declare function copyTextToClipboard(data: any): Promise<boolean>;
|
|
3
1
|
/**
|
|
4
2
|
* export the experiments result in JSON format
|
|
5
|
-
* @param
|
|
3
|
+
* @param data
|
|
4
|
+
* @param fileName
|
|
5
|
+
* @param spaceIndent
|
|
6
|
+
* @param isCompressed
|
|
6
7
|
*/
|
|
7
8
|
declare function exportAsJSON(data: any, fileName?: string, spaceIndent?: number, isCompressed?: boolean): Promise<void>;
|
|
8
9
|
declare function exportAsMatrix(data: any, fileName?: string): void;
|
|
@@ -20,4 +21,4 @@ export interface BlobObject {
|
|
|
20
21
|
height: number;
|
|
21
22
|
}
|
|
22
23
|
declare function getBlob(rootRef: HTMLDivElement, elementID: string): BlobObject;
|
|
23
|
-
export { exportAsSVG, exportAsJSON, exportAsNMRE, exportAsPng, copyPNGToClipboard,
|
|
24
|
+
export { exportAsSVG, exportAsJSON, exportAsNMRE, exportAsPng, copyPNGToClipboard, exportAsMol, exportAsMatrix, getBlob, };
|
|
@@ -1,40 +1,12 @@
|
|
|
1
1
|
import { saveAs } from 'file-saver';
|
|
2
2
|
import JSZip from 'jszip';
|
|
3
|
-
|
|
4
|
-
return copyToClipboard(data, 'text/html');
|
|
5
|
-
}
|
|
6
|
-
async function copyTextToClipboard(data) {
|
|
7
|
-
return copyToClipboard(data, 'text/plain');
|
|
8
|
-
}
|
|
9
|
-
async function copyToClipboard(data, type) {
|
|
10
|
-
try {
|
|
11
|
-
if (typeof ClipboardItem !== 'undefined') {
|
|
12
|
-
void navigator.clipboard.write([
|
|
13
|
-
new ClipboardItem({
|
|
14
|
-
[type]: new Promise((resolve) => {
|
|
15
|
-
resolve(new Blob([data], { type }));
|
|
16
|
-
}),
|
|
17
|
-
}),
|
|
18
|
-
]);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
const clipboard = await import('clipboard-polyfill');
|
|
22
|
-
//TODO when Firefox team implement ClipboardItem this code should be removed
|
|
23
|
-
// this library is used mainly to solve the problem of copy HTML to a clipboard in Firefox but we use it for both HTML and plain text
|
|
24
|
-
const item = new clipboard.ClipboardItem({
|
|
25
|
-
[type]: new Blob([data], { type }),
|
|
26
|
-
});
|
|
27
|
-
await clipboard.write([item]);
|
|
28
|
-
}
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
3
|
+
import { newClipboardItem, write } from '../../utils/clipboard/clipboard';
|
|
35
4
|
/**
|
|
36
5
|
* export the experiments result in JSON format
|
|
37
|
-
* @param
|
|
6
|
+
* @param data
|
|
7
|
+
* @param fileName
|
|
8
|
+
* @param spaceIndent
|
|
9
|
+
* @param isCompressed
|
|
38
10
|
*/
|
|
39
11
|
async function exportAsJSON(data, fileName = 'experiment', spaceIndent = 0, isCompressed = false) {
|
|
40
12
|
const fileData = JSON.stringify(data, (key, value) => ArrayBuffer.isView(value) ? Array.from(value) : value, spaceIndent);
|
|
@@ -137,16 +109,24 @@ function copyDataURLClipboardFireFox(image) {
|
|
|
137
109
|
}
|
|
138
110
|
function copyBlobToClipboard(canvas) {
|
|
139
111
|
canvas.toBlob((b) => {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
112
|
+
if (!b)
|
|
113
|
+
return;
|
|
114
|
+
(async () => {
|
|
115
|
+
const clip = await newClipboardItem({
|
|
116
|
+
[b.type]: b,
|
|
117
|
+
});
|
|
118
|
+
await write([clip]);
|
|
119
|
+
})()
|
|
120
|
+
.catch(() => {
|
|
121
|
+
const png = canvas.toDataURL('image/png', 1);
|
|
122
|
+
copyDataURLClipboardFireFox(png);
|
|
123
|
+
URL.revokeObjectURL(png);
|
|
124
|
+
})
|
|
125
|
+
.then(() => {
|
|
144
126
|
// eslint-disable-next-line no-console
|
|
145
127
|
console.log('experiment copied.');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
reportError(error);
|
|
149
|
-
});
|
|
128
|
+
})
|
|
129
|
+
.catch(reportError);
|
|
150
130
|
});
|
|
151
131
|
}
|
|
152
132
|
function copyPNGToClipboard(rootRef, elementID) {
|
|
@@ -162,17 +142,9 @@ function copyPNGToClipboard(rootRef, elementID) {
|
|
|
162
142
|
}
|
|
163
143
|
let img = new Image();
|
|
164
144
|
const url = URL.createObjectURL(blob);
|
|
165
|
-
img.addEventListener('load',
|
|
145
|
+
img.addEventListener('load', () => {
|
|
166
146
|
context?.drawImage(img, 0, 0);
|
|
167
|
-
|
|
168
|
-
// @ts-expect-error write exists in some browsers
|
|
169
|
-
if (navigator.clipboard.write) {
|
|
170
|
-
copyBlobToClipboard(canvas);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
copyDataURLClipboardFireFox(png);
|
|
174
|
-
}
|
|
175
|
-
URL.revokeObjectURL(png);
|
|
147
|
+
copyBlobToClipboard(canvas);
|
|
176
148
|
});
|
|
177
149
|
img.src = url;
|
|
178
150
|
}
|
|
@@ -234,4 +206,4 @@ function getMoleculesElement(rootRef) {
|
|
|
234
206
|
}
|
|
235
207
|
return floatingMoleculesGroup;
|
|
236
208
|
}
|
|
237
|
-
export { exportAsSVG, exportAsJSON, exportAsNMRE, exportAsPng, copyPNGToClipboard,
|
|
209
|
+
export { exportAsSVG, exportAsJSON, exportAsNMRE, exportAsPng, copyPNGToClipboard, exportAsMol, exportAsMatrix, getBlob, };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function readText(): Promise<string>;
|
|
2
|
+
export declare function read(): Promise<ClipboardItems>;
|
|
3
|
+
export declare function writeText(data: string): Promise<void>;
|
|
4
|
+
export interface ClipboardItemInterface {
|
|
5
|
+
readonly presentationStyle?: PresentationStyle;
|
|
6
|
+
readonly lastModified?: number;
|
|
7
|
+
readonly delayed?: boolean;
|
|
8
|
+
readonly types: ReadonlyArray<string>;
|
|
9
|
+
getType(type: string): Promise<Blob>;
|
|
10
|
+
}
|
|
11
|
+
export declare function write(data: ClipboardItemInterface[]): Promise<void>;
|
|
12
|
+
export declare function newClipboardItem(items: Record<string, string | Blob | PromiseLike<string | Blob>>, options?: ClipboardItemOptions): Promise<ClipboardItem>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function readText() {
|
|
2
|
+
return import('clipboard-polyfill').then((c) => c.readText());
|
|
3
|
+
}
|
|
4
|
+
export function read() {
|
|
5
|
+
return import('clipboard-polyfill').then((c) => c.read());
|
|
6
|
+
}
|
|
7
|
+
export function writeText(data) {
|
|
8
|
+
return import('clipboard-polyfill').then((c) => c.writeText(data));
|
|
9
|
+
}
|
|
10
|
+
export function write(data) {
|
|
11
|
+
return import('clipboard-polyfill').then((c) => c.write(data));
|
|
12
|
+
}
|
|
13
|
+
export function newClipboardItem(items, options) {
|
|
14
|
+
return import('clipboard-polyfill').then((c) => new c.ClipboardItem(items, options));
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ClipboardMode } from './types';
|
|
2
|
+
export interface CFBP {
|
|
3
|
+
mode: ClipboardMode | undefined;
|
|
4
|
+
label?: string;
|
|
5
|
+
onDismiss: () => void;
|
|
6
|
+
}
|
|
7
|
+
export interface ClipboardFallbackProps extends CFBP, Partial<Omit<ClipboardFallbackReadProps, 'mode' | 'label' | 'onDismiss'>>, Partial<Omit<ClipboardFallbackReadTextProps, 'mode' | 'label' | 'onDismiss'>>, Partial<Omit<ClipboardFallbackWriteProps, 'mode' | 'label' | 'onDismiss'>>, Partial<Omit<ClipboardFallbackWriteTextProps, 'mode' | 'label' | 'onDismiss'>> {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* switch between ClipboardFallback variant over mode props
|
|
11
|
+
* mode === read => ClipboardFallbackReadProps
|
|
12
|
+
* mode === readText => ClipboardFallbackReadTextProps
|
|
13
|
+
* mode === write => ClipboardFallbackWriteProps
|
|
14
|
+
* mode === writeText => ClipboardFallbackWriteTextProps
|
|
15
|
+
*
|
|
16
|
+
* implement what you need in function what part of the clipboard api from useClipboard you use
|
|
17
|
+
* props will be checked at runtime
|
|
18
|
+
*
|
|
19
|
+
* @param props
|
|
20
|
+
*/
|
|
21
|
+
export declare function ClipboardFallback(props: ClipboardFallbackProps): import("react/jsx-runtime").JSX.Element | null;
|
|
22
|
+
/**
|
|
23
|
+
* return null if !props.mode
|
|
24
|
+
*
|
|
25
|
+
* @param props
|
|
26
|
+
* @see ClipboardFallback
|
|
27
|
+
*/
|
|
28
|
+
export declare function ClipboardFallbackModal(props: ClipboardFallbackProps): import("react/jsx-runtime").JSX.Element | null;
|
|
29
|
+
interface ClipboardFallbackReadProps extends CFBP {
|
|
30
|
+
onRead: (file: File) => void;
|
|
31
|
+
mode: 'read';
|
|
32
|
+
}
|
|
33
|
+
interface ClipboardFallbackReadTextProps extends CFBP {
|
|
34
|
+
onReadText: (text: string) => void;
|
|
35
|
+
mode: 'readText';
|
|
36
|
+
}
|
|
37
|
+
interface ClipboardFallbackWriteProps extends CFBP {
|
|
38
|
+
file: File;
|
|
39
|
+
mode: 'write';
|
|
40
|
+
}
|
|
41
|
+
interface ClipboardFallbackWriteTextProps extends CFBP {
|
|
42
|
+
text: string;
|
|
43
|
+
mode: 'writeText';
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import styled from '@emotion/styled';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import { Modal } from 'react-science/ui';
|
|
5
|
+
import Button from '../../component/elements/Button';
|
|
6
|
+
const ClipboardForm = styled.form `
|
|
7
|
+
padding: 0.5rem;
|
|
8
|
+
|
|
9
|
+
& > label {
|
|
10
|
+
display: flex;
|
|
11
|
+
flex-direction: column;
|
|
12
|
+
gap: 0.5rem;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
textarea {
|
|
16
|
+
padding: 0.5rem;
|
|
17
|
+
}
|
|
18
|
+
`;
|
|
19
|
+
const Actions = styled.div `
|
|
20
|
+
display: flex;
|
|
21
|
+
gap: 0.5rem;
|
|
22
|
+
justify-content: flex-end;
|
|
23
|
+
`;
|
|
24
|
+
function throwError(props, prop) {
|
|
25
|
+
throw new Error(`props.${prop} is mandatory with ${String(props.mode)} mode`);
|
|
26
|
+
}
|
|
27
|
+
function assertReadProps(props) {
|
|
28
|
+
if (typeof props.onRead !== 'function')
|
|
29
|
+
throwError(props, 'onRead');
|
|
30
|
+
}
|
|
31
|
+
function assertReadTextProps(props) {
|
|
32
|
+
if (typeof props.onReadText !== 'function')
|
|
33
|
+
throwError(props, 'onReadText');
|
|
34
|
+
}
|
|
35
|
+
function assertWriteProps(props) {
|
|
36
|
+
if (!props.file)
|
|
37
|
+
throwError(props, 'file');
|
|
38
|
+
if (typeof props.onDismiss !== 'function')
|
|
39
|
+
throwError(props, 'onDismiss');
|
|
40
|
+
}
|
|
41
|
+
function assertWriteTextProps(props) {
|
|
42
|
+
if (typeof props.text !== 'string')
|
|
43
|
+
throwError(props, 'text');
|
|
44
|
+
if (typeof props.onDismiss !== 'function')
|
|
45
|
+
throwError(props, 'onDismiss');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* switch between ClipboardFallback variant over mode props
|
|
49
|
+
* mode === read => ClipboardFallbackReadProps
|
|
50
|
+
* mode === readText => ClipboardFallbackReadTextProps
|
|
51
|
+
* mode === write => ClipboardFallbackWriteProps
|
|
52
|
+
* mode === writeText => ClipboardFallbackWriteTextProps
|
|
53
|
+
*
|
|
54
|
+
* implement what you need in function what part of the clipboard api from useClipboard you use
|
|
55
|
+
* props will be checked at runtime
|
|
56
|
+
*
|
|
57
|
+
* @param props
|
|
58
|
+
*/
|
|
59
|
+
export function ClipboardFallback(props) {
|
|
60
|
+
switch (props.mode) {
|
|
61
|
+
case 'read':
|
|
62
|
+
assertReadProps(props);
|
|
63
|
+
return _jsx(ClipboardFallbackRead, { ...props });
|
|
64
|
+
case 'readText':
|
|
65
|
+
assertReadTextProps(props);
|
|
66
|
+
return _jsx(ClipboardFallbackReadText, { ...props });
|
|
67
|
+
case 'write': {
|
|
68
|
+
assertWriteProps(props);
|
|
69
|
+
return _jsx(ClipboardFallbackWrite, { ...props });
|
|
70
|
+
}
|
|
71
|
+
case 'writeText':
|
|
72
|
+
assertWriteTextProps(props);
|
|
73
|
+
return _jsx(ClipboardFallbackWriteText, { ...props });
|
|
74
|
+
default:
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* return null if !props.mode
|
|
80
|
+
*
|
|
81
|
+
* @param props
|
|
82
|
+
* @see ClipboardFallback
|
|
83
|
+
*/
|
|
84
|
+
export function ClipboardFallbackModal(props) {
|
|
85
|
+
if (!props.mode)
|
|
86
|
+
return null;
|
|
87
|
+
const titles = {
|
|
88
|
+
read: `Import data from file`,
|
|
89
|
+
readText: `Paste text from clipboard`,
|
|
90
|
+
write: `Export data to file`,
|
|
91
|
+
writeText: `Copy text from text area`,
|
|
92
|
+
};
|
|
93
|
+
return (_jsxs(Modal, { hasCloseButton: true, isOpen: true, onRequestClose: props.onDismiss, children: [_jsx(Modal.Header, { children: _jsx("h2", { children: titles[props.mode] }) }), _jsx(Modal.Body, { children: _jsx(ClipboardFallback, { ...props }) })] }));
|
|
94
|
+
}
|
|
95
|
+
function ClipboardFallbackRead(props) {
|
|
96
|
+
const onFileRef = useRef(props.onRead);
|
|
97
|
+
onFileRef.current = props.onRead;
|
|
98
|
+
const onSubmit = useCallback((event) => {
|
|
99
|
+
event.preventDefault();
|
|
100
|
+
const data = new FormData(event.currentTarget);
|
|
101
|
+
const file = data.get('file');
|
|
102
|
+
if (!(file instanceof File))
|
|
103
|
+
return;
|
|
104
|
+
onFileRef.current(file);
|
|
105
|
+
}, []);
|
|
106
|
+
if (props.mode !== 'read')
|
|
107
|
+
return null;
|
|
108
|
+
return (_jsxs(ClipboardForm, { onSubmit: onSubmit, children: [_jsxs("label", { children: [_jsx("span", { children: props.label ?? 'File' }), _jsx("input", { name: "file", type: "file" })] }), _jsx("p", { children: "We were not able to read from your clipboard. Please input your data in the file field" }), _jsxs(Actions, { children: [_jsx(Button.Done, { type: "submit", children: "Submit" }), _jsx(Button.Secondary, { type: "reset", onClick: props.onDismiss, children: "Cancel" })] })] }));
|
|
109
|
+
}
|
|
110
|
+
function ClipboardFallbackReadText(props) {
|
|
111
|
+
const onTextRef = useRef(props.onReadText);
|
|
112
|
+
onTextRef.current = props.onReadText;
|
|
113
|
+
const onSubmit = useCallback((event) => {
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
const data = new FormData(event.currentTarget);
|
|
116
|
+
const text = data.get('text');
|
|
117
|
+
if (typeof text !== 'string')
|
|
118
|
+
return;
|
|
119
|
+
onTextRef.current(text);
|
|
120
|
+
}, []);
|
|
121
|
+
if (props.mode !== 'readText')
|
|
122
|
+
return null;
|
|
123
|
+
return (_jsxs(ClipboardForm, { onSubmit: onSubmit, children: [_jsxs("label", { children: [_jsx("span", { children: props.label ?? 'Text' }), _jsx("textarea", { name: "text", cols: 50, rows: 10 })] }), _jsx("p", { children: "We were not able to read text from your clipboard. Please paste your data in the text area." }), _jsxs(Actions, { children: [_jsx(Button.Done, { type: "submit", children: "Submit" }), _jsx(Button.Secondary, { type: "reset", onClick: props.onDismiss, children: "Cancel" })] })] }));
|
|
124
|
+
}
|
|
125
|
+
function ClipboardFallbackWrite(props) {
|
|
126
|
+
const onDismissRef = useRef(props.onDismiss);
|
|
127
|
+
onDismissRef.current = props.onDismiss;
|
|
128
|
+
const onSubmit = useCallback((event) => {
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
onDismissRef.current();
|
|
131
|
+
}, []);
|
|
132
|
+
const downloadLink = useMemo(() => URL.createObjectURL(props.file), [props.file]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
return () => URL.revokeObjectURL(downloadLink);
|
|
135
|
+
}, [downloadLink]);
|
|
136
|
+
if (props.mode !== 'write')
|
|
137
|
+
return null;
|
|
138
|
+
return (_jsxs(ClipboardForm, { onSubmit: onSubmit, children: [_jsx("p", { children: props.label ?? 'Download' }), _jsx("a", { href: downloadLink, download: props.file.name, children: "We were not able to write data to your clipboard. Please download your data with this link." }), _jsx(Actions, { children: _jsx(Button.Secondary, { type: "submit", children: "Dismiss" }) })] }));
|
|
139
|
+
}
|
|
140
|
+
function ClipboardFallbackWriteText(props) {
|
|
141
|
+
const onDismissRef = useRef(props.onDismiss);
|
|
142
|
+
onDismissRef.current = props.onDismiss;
|
|
143
|
+
const onSubmit = useCallback((event) => {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
onDismissRef.current();
|
|
146
|
+
}, []);
|
|
147
|
+
if (props.mode !== 'writeText')
|
|
148
|
+
return null;
|
|
149
|
+
return (_jsxs(ClipboardForm, { onSubmit: onSubmit, children: [_jsx("span", { children: props.label ?? 'Text' }), _jsx("textarea", { name: "text", cols: 30, rows: 10, ref: (node) => node?.select(), children: props.text }), _jsx("p", { children: "We were not able to write text to your clipboard. Please copy your data in the text area." }), _jsx(Actions, { children: _jsx(Button.Secondary, { type: "submit", children: "Dismiss" }) })] }));
|
|
150
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ClipboardMode } from './types';
|
|
2
|
+
type PN = PermissionName | 'clipboard-read' | 'clipboard-write';
|
|
3
|
+
export interface UsePermissionReturn {
|
|
4
|
+
state: PermissionState | undefined;
|
|
5
|
+
isGranted: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function useNavigatorPermission(now: boolean, permissionName: PN, onGranted?: () => void, onError?: typeof reportError): UsePermissionReturn;
|
|
8
|
+
export interface UseClipboardReturn {
|
|
9
|
+
/**
|
|
10
|
+
* polyfill native api (stable)
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Fallback on 'read' mode
|
|
14
|
+
*/
|
|
15
|
+
read: () => Promise<ClipboardItems | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Fallback on 'readText' mode
|
|
18
|
+
*/
|
|
19
|
+
readText: () => Promise<string | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Fallback on 'write' mode with blobs (get blob for each first type for each item) state
|
|
22
|
+
*/
|
|
23
|
+
write: (data: ClipboardItems) => Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Fallback on 'writeText' mode with text state
|
|
26
|
+
*/
|
|
27
|
+
writeText: (data: string) => Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* custom helper (stable)
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* use write but no need do to manually ClipboardItems transform
|
|
33
|
+
* Fallback on 'writeText' mode with text state
|
|
34
|
+
* @param data
|
|
35
|
+
* @param type a mime type default to 'text/plain'
|
|
36
|
+
*/
|
|
37
|
+
rawWriteWithType: (data: string, type?: 'text/html' | 'text/plain' | string) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* state for alternative
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* state to know if you need to display an alternative interface
|
|
43
|
+
*/
|
|
44
|
+
shouldFallback: ClipboardMode | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* state for 'writeText' / 'rawWriteWithType' mode fallback
|
|
47
|
+
*/
|
|
48
|
+
text: string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* state for 'write' mode fallback
|
|
51
|
+
*/
|
|
52
|
+
blobs: Blob[] | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* method to clean up state for alternative interface (set shouldFallback to undefined, text to undefined and blobs to undefined)
|
|
55
|
+
* stable
|
|
56
|
+
*/
|
|
57
|
+
cleanShouldFallback: () => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Give you a wrapped clipboard api and a shouldFallback state.
|
|
61
|
+
* ignore if read and readText return promise filled with undefined, and rely on shouldFallback to display an alternative interaction.
|
|
62
|
+
* You should use ClipboardFallback component
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```tsx
|
|
66
|
+
* const { readText, shouldFallback, setShouldFallback } = useClipboard();
|
|
67
|
+
*
|
|
68
|
+
* function handlePasteAction() {
|
|
69
|
+
* // yes you can safely ignore error here,
|
|
70
|
+
* // readText() will never throw, error is caught and set shouldFallback to corresponding mode
|
|
71
|
+
* void readText().then(handlePaste);
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* function handlePaste(text: string | undefined) {
|
|
75
|
+
* if (!text) return;
|
|
76
|
+
*
|
|
77
|
+
* setState(text);
|
|
78
|
+
* setShouldFallback(null);
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* return (
|
|
82
|
+
* <>
|
|
83
|
+
* <button onClick={handlePasteAction}>Paste</button>
|
|
84
|
+
* <ClipboardFallbackModal
|
|
85
|
+
* mode={shouldFallback}
|
|
86
|
+
* onDismiss={() => setShouldFallback(null)}
|
|
87
|
+
* onReadText={handlePaste}
|
|
88
|
+
* />
|
|
89
|
+
* </>
|
|
90
|
+
* )
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function useClipboard(): UseClipboardReturn;
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { newClipboardItem, read, readText, write, writeText, } from './clipboard';
|
|
3
|
+
export function useNavigatorPermission(now, permissionName, onGranted, onError = reportError) {
|
|
4
|
+
const [state, setState] = useState();
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
if (!now)
|
|
7
|
+
return;
|
|
8
|
+
let canceled = false;
|
|
9
|
+
let status;
|
|
10
|
+
function onPermissionChange() {
|
|
11
|
+
if (canceled)
|
|
12
|
+
return;
|
|
13
|
+
setState(status.state);
|
|
14
|
+
if (status.state === 'granted') {
|
|
15
|
+
onGranted?.();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
void navigator.permissions
|
|
19
|
+
.query({ name: permissionName })
|
|
20
|
+
.then((pStatus) => {
|
|
21
|
+
if (canceled)
|
|
22
|
+
return;
|
|
23
|
+
status = pStatus;
|
|
24
|
+
setState(status.state);
|
|
25
|
+
status.addEventListener('change', onPermissionChange);
|
|
26
|
+
})
|
|
27
|
+
.catch((error) => {
|
|
28
|
+
if (canceled)
|
|
29
|
+
return;
|
|
30
|
+
onError(error);
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
canceled = true;
|
|
34
|
+
if (status) {
|
|
35
|
+
status.removeEventListener('change', onPermissionChange);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}, [now, permissionName, onGranted, onError]);
|
|
39
|
+
return { state, isGranted: state === 'granted' };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Give you a wrapped clipboard api and a shouldFallback state.
|
|
43
|
+
* ignore if read and readText return promise filled with undefined, and rely on shouldFallback to display an alternative interaction.
|
|
44
|
+
* You should use ClipboardFallback component
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* const { readText, shouldFallback, setShouldFallback } = useClipboard();
|
|
49
|
+
*
|
|
50
|
+
* function handlePasteAction() {
|
|
51
|
+
* // yes you can safely ignore error here,
|
|
52
|
+
* // readText() will never throw, error is caught and set shouldFallback to corresponding mode
|
|
53
|
+
* void readText().then(handlePaste);
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* function handlePaste(text: string | undefined) {
|
|
57
|
+
* if (!text) return;
|
|
58
|
+
*
|
|
59
|
+
* setState(text);
|
|
60
|
+
* setShouldFallback(null);
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* return (
|
|
64
|
+
* <>
|
|
65
|
+
* <button onClick={handlePasteAction}>Paste</button>
|
|
66
|
+
* <ClipboardFallbackModal
|
|
67
|
+
* mode={shouldFallback}
|
|
68
|
+
* onDismiss={() => setShouldFallback(null)}
|
|
69
|
+
* onReadText={handlePaste}
|
|
70
|
+
* />
|
|
71
|
+
* </>
|
|
72
|
+
* )
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function useClipboard() {
|
|
76
|
+
const [shouldFallback, setShouldFallback] = useState();
|
|
77
|
+
const [text, setText] = useState();
|
|
78
|
+
const [blobs, setBlobs] = useState();
|
|
79
|
+
const cleanShouldFallback = useCallback(() => {
|
|
80
|
+
setShouldFallback(undefined);
|
|
81
|
+
setText(undefined);
|
|
82
|
+
setBlobs(undefined);
|
|
83
|
+
}, []);
|
|
84
|
+
const clipboardAPI = useMemo(() => ({
|
|
85
|
+
async read() {
|
|
86
|
+
try {
|
|
87
|
+
return await read();
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
setShouldFallback('read');
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
async readText() {
|
|
94
|
+
try {
|
|
95
|
+
return await readText();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
setShouldFallback('readText');
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
async write(data) {
|
|
102
|
+
try {
|
|
103
|
+
return await write(data);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
const blobs = await Promise.all(data.map((ci) => ci.getType(ci.types[0])));
|
|
107
|
+
setBlobs(blobs);
|
|
108
|
+
setShouldFallback('write');
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
async rawWriteWithType(data, type = 'text/plain') {
|
|
112
|
+
try {
|
|
113
|
+
const item = await newClipboardItem({
|
|
114
|
+
[type]: new Promise((resolve) => {
|
|
115
|
+
resolve(new Blob([data], { type }));
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
return await write([item]);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
setText(data);
|
|
122
|
+
setShouldFallback('writeText');
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async writeText(data) {
|
|
126
|
+
try {
|
|
127
|
+
return await writeText(data);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
setText(data);
|
|
131
|
+
setShouldFallback('writeText');
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
}), []);
|
|
135
|
+
return {
|
|
136
|
+
...clipboardAPI,
|
|
137
|
+
shouldFallback,
|
|
138
|
+
text,
|
|
139
|
+
blobs,
|
|
140
|
+
cleanShouldFallback,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ClipboardMode = 'read' | 'readText' | 'write' | 'writeText';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/versionInfo.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default { version: 'v0.
|
|
1
|
+
export default { version: 'v0.40.1' };
|