Skip to main content

Copy text

This case is from Peng, a three-party developer 」

1. Usage scenarios

This example describes how to copy text in YIDA.

2. Implement functions

2.1. Create a custom page

2.2. Text copy function

/**
* 文本复制
* @param text 需要复制的内容
*/
export function copyToClip(text) {
if (this.utils.isMobile() && window.dd) {
dd.biz.clipboardData.setData({
text,
onSuccess: () => {
this.utils.toast({
title: '复制成功',
type: 'success',
});
},
onFail: () => {
this.utils.toast({
title: '复制失败',
type: 'error',
});
},
})
} else {
if (navigator.clipboard && window.isSecureContext && typeof navigator.clipboard.writeText === 'function') {
navigator.clipboard.writeText(text).then(() => {
this.utils.toast({
title: '复制成功',
type: 'success',
});
}).catch(() => {
this.utils.toast({
title: '复制失败',
type: 'error',
});
});
} else {
try {
const input = document.createElement('input');
input.readOnly = 'readonly';
input.value = text;
document.body.appendChild(input);
input.select();
input.setSelectionRange(0, input.value.length);
document.execCommand('Copy');
document.body.removeChild(input);
this.utils.toast({
title: '复制成功',
type: 'success',
});
} catch (err) {
this.utils.toast({
title: '复制失败',
type: 'error',
});
}
}
}
}

2.3. Function usage

Example:

// 点击复制
export function onClick() {
this.copyToClip(this.$('textareaField_l9pdlt0k').getValue());
}

3. Effect

4. Try it online

This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
Copyright © 2024钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4