mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
Copy to clipboard action support
This commit is contained in:
@@ -11988,6 +11988,7 @@
|
||||
"client/web/divkit/package.json":"divkit/public/client/web/divkit/package.json",
|
||||
"client/web/divkit/server/package.json":"divkit/public/client/web/divkit/server/package.json",
|
||||
"client/web/divkit/src/actions/array.ts":"divkit/public/client/web/divkit/src/actions/array.ts",
|
||||
"client/web/divkit/src/actions/copyToClipboard.ts":"divkit/public/client/web/divkit/src/actions/copyToClipboard.ts",
|
||||
"client/web/divkit/src/client-devtool.ts":"divkit/public/client/web/divkit/src/client-devtool.ts",
|
||||
"client/web/divkit/src/client.ts":"divkit/public/client/web/divkit/src/client.ts",
|
||||
"client/web/divkit/src/components.js":"divkit/public/client/web/divkit/src/components.js",
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { ActionCopyToClipboard, WrappedError } from '../../typings/common';
|
||||
import type { MaybeMissing } from '../expressions/json';
|
||||
import { wrapError } from '../utils/wrapError';
|
||||
|
||||
export function copyToClipboard(
|
||||
logError: (error: WrappedError) => void,
|
||||
actionTyped: MaybeMissing<ActionCopyToClipboard>
|
||||
): void {
|
||||
if (!(
|
||||
actionTyped.content && (actionTyped.content.type === 'text' || actionTyped.content.type === 'url') &&
|
||||
typeof actionTyped.content.value === 'string'
|
||||
)) {
|
||||
logError(wrapError(new Error('Incorrect action'), {
|
||||
additional: {
|
||||
action: actionTyped
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(
|
||||
typeof navigator !== 'undefined' &&
|
||||
'clipboard' in navigator &&
|
||||
navigator.clipboard &&
|
||||
'write' in navigator.clipboard &&
|
||||
typeof navigator.clipboard.writeText === 'function'
|
||||
)) {
|
||||
logError(wrapError(new Error('Clipboard is unavailable'), {
|
||||
additional: {
|
||||
action: actionTyped
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
navigator.clipboard.writeText(actionTyped.content.value).catch(err => {
|
||||
logError(wrapError(new Error('Failed to copy to the clipboard'), {
|
||||
additional: {
|
||||
originalError: String(err)
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
@@ -72,6 +72,7 @@
|
||||
import { getUrlSchema, isBuiltinSchema } from '../utils/url';
|
||||
import { TimersController } from '../utils/timers';
|
||||
import { arrayInsert, arrayRemove } from '../actions/array';
|
||||
import { copyToClipboard } from '../actions/copyToClipboard';
|
||||
|
||||
export let id: string;
|
||||
export let json: Partial<DivJson> = {};
|
||||
@@ -772,6 +773,9 @@
|
||||
case 'array_remove_value':
|
||||
arrayRemove(variables, logError, actionTyped);
|
||||
break;
|
||||
case 'copy_to_clipboard':
|
||||
copyToClipboard(logError, actionTyped);
|
||||
break;
|
||||
default: {
|
||||
logError(wrapError(new Error('Unknown type of action'), {
|
||||
additional: {
|
||||
|
||||
+19
-1
@@ -160,7 +160,25 @@ export interface ActionArrayInsertValue {
|
||||
value: TypedValue;
|
||||
}
|
||||
|
||||
export type TypedAction = ActionSetVariable | ActionArrayRemoveValue | ActionArrayInsertValue;
|
||||
export interface CopyToClipboardContentText {
|
||||
type: 'text';
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface CopyToClipboardContentUrl {
|
||||
type: 'url';
|
||||
value: string;
|
||||
}
|
||||
|
||||
export type CopyToClipboardContent = CopyToClipboardContentText | CopyToClipboardContentUrl;
|
||||
|
||||
export interface ActionCopyToClipboard {
|
||||
type: 'copy_to_clipboard';
|
||||
content: CopyToClipboardContent;
|
||||
}
|
||||
|
||||
export type TypedAction = ActionSetVariable | ActionArrayRemoveValue | ActionArrayInsertValue |
|
||||
ActionCopyToClipboard;
|
||||
|
||||
export interface Action {
|
||||
log_id: string;
|
||||
|
||||
@@ -16,11 +16,7 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"$ref": "div-action-copy-to-clipboard.json",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
]
|
||||
"$ref": "div-action-copy-to-clipboard.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@
|
||||
"tags": [
|
||||
"DivAction"
|
||||
],
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
],
|
||||
"steps": [
|
||||
"Tap to copy url button and paste clipboard to input",
|
||||
"Tap to copy string button and paste clipboard to input"
|
||||
|
||||
Reference in New Issue
Block a user