mirror of https://github.com/fantasticit/think.git
fix: fix copy text
This commit is contained in:
parent
c03d1ab00d
commit
009d7782a4
|
@ -3,143 +3,66 @@
|
||||||
|
|
||||||
var deselectCurrent = require('toggle-selection');
|
var deselectCurrent = require('toggle-selection');
|
||||||
|
|
||||||
var clipboardToIE11Formatting = {
|
function copy(text, onCopy) {
|
||||||
'text/plain': 'Text',
|
var reselectPrevious,
|
||||||
'text/html': 'Url',
|
|
||||||
'default': 'Text',
|
|
||||||
};
|
|
||||||
|
|
||||||
var defaultMessage = 'Copy to clipboard: #{key}, Enter';
|
|
||||||
|
|
||||||
function format(message) {
|
|
||||||
var copyKey = (/mac os x/i.test(navigator.userAgent) ? '⌘' : 'Ctrl') + '+C';
|
|
||||||
return message.replace(/#{\s*key\s*}/g, copyKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
function copy(text, options) {
|
|
||||||
var debug,
|
|
||||||
message,
|
|
||||||
reselectPrevious,
|
|
||||||
range,
|
range,
|
||||||
selection,
|
selection,
|
||||||
mark,
|
mark,
|
||||||
success = false;
|
success = false;
|
||||||
if (!options) {
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
debug = options.debug || false;
|
|
||||||
try {
|
|
||||||
reselectPrevious = deselectCurrent();
|
|
||||||
|
|
||||||
range = document.createRange();
|
reselectPrevious = deselectCurrent();
|
||||||
selection = document.getSelection();
|
range = document.createRange();
|
||||||
|
selection = document.getSelection();
|
||||||
|
|
||||||
mark = document.createElement('span');
|
mark = document.createElement('span');
|
||||||
mark.textContent = text;
|
mark.textContent = text;
|
||||||
// reset user styles for span element
|
mark.style.all = 'unset';
|
||||||
mark.style.all = 'unset';
|
mark.style.position = 'fixed';
|
||||||
// prevents scrolling to the end of the page
|
mark.style.top = 0;
|
||||||
mark.style.position = 'fixed';
|
mark.style.clip = 'rect(0, 0, 0, 0)';
|
||||||
mark.style.top = 0;
|
mark.style.whiteSpace = 'pre';
|
||||||
mark.style.clip = 'rect(0, 0, 0, 0)';
|
mark.style.webkitUserSelect = 'text';
|
||||||
// used to preserve spaces and line breaks
|
mark.style.MozUserSelect = 'text';
|
||||||
mark.style.whiteSpace = 'pre';
|
mark.style.msUserSelect = 'text';
|
||||||
// do not inherit user-select (it may be `none`)
|
mark.style.userSelect = 'text';
|
||||||
mark.style.webkitUserSelect = 'text';
|
mark.addEventListener('copy', function (e) {
|
||||||
mark.style.MozUserSelect = 'text';
|
var data = [];
|
||||||
mark.style.msUserSelect = 'text';
|
|
||||||
mark.style.userSelect = 'text';
|
if (typeof text === 'string') {
|
||||||
mark.addEventListener('copy', function (e) {
|
data = [{ format: 'text/plain', text: text }];
|
||||||
e.stopPropagation();
|
} else if (Array.isArray(text)) {
|
||||||
if (options.format) {
|
text.forEach(function (item) {
|
||||||
e.preventDefault();
|
data.push({
|
||||||
if (typeof e.clipboardData === 'undefined') {
|
format: item.format || 'text/plain',
|
||||||
// IE 11
|
text: item.text || item,
|
||||||
debug && console.warn('unable to use e.clipboardData');
|
});
|
||||||
debug && console.warn('trying IE specific stuff');
|
});
|
||||||
window.clipboardData.clearData();
|
} else {
|
||||||
var format = clipboardToIE11Formatting[options.format] || clipboardToIE11Formatting['default'];
|
data.push({
|
||||||
if (Array.isArray(text)) {
|
format: 'text/plain',
|
||||||
text.forEach(function (item) {
|
text: text,
|
||||||
if (typeof item === 'string') {
|
});
|
||||||
window.clipboardData.setData(item, item);
|
}
|
||||||
} else {
|
|
||||||
window.clipboardData.setData(item.format || format, item.text || item);
|
data.forEach(function (item) {
|
||||||
}
|
e.clipboardData.setData(item.format, item.text);
|
||||||
});
|
|
||||||
} else {
|
|
||||||
window.clipboardData.setData(format, text);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// all other browsers
|
|
||||||
e.clipboardData.clearData();
|
|
||||||
if (Array.isArray(text)) {
|
|
||||||
text.forEach(function (item) {
|
|
||||||
if (typeof item === 'string') {
|
|
||||||
e.clipboardData.setData(item, item);
|
|
||||||
} else {
|
|
||||||
e.clipboardData.setData(item.format || format, item.text || item);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
e.clipboardData.setData(format, text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (options.onCopy) {
|
|
||||||
e.preventDefault();
|
|
||||||
options.onCopy(e.clipboardData);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
document.body.appendChild(mark);
|
e.preventDefault();
|
||||||
|
|
||||||
range.selectNodeContents(mark);
|
onCopy && onCopy();
|
||||||
selection.addRange(range);
|
});
|
||||||
|
|
||||||
var successful = document.execCommand('copy');
|
document.body.appendChild(mark);
|
||||||
if (!successful) {
|
range.selectNodeContents(mark);
|
||||||
throw new Error('copy command was unsuccessful');
|
selection.addRange(range);
|
||||||
}
|
var successful = document.execCommand('copy');
|
||||||
success = true;
|
|
||||||
} catch (err) {
|
|
||||||
debug && console.error('unable to copy using execCommand: ', err);
|
|
||||||
debug && console.warn('trying IE specific stuff');
|
|
||||||
try {
|
|
||||||
if (Array.isArray(text)) {
|
|
||||||
text.forEach(function (item) {
|
|
||||||
if (typeof item === 'string') {
|
|
||||||
window.clipboardData.setData(item, item);
|
|
||||||
} else {
|
|
||||||
window.clipboardData.setData(item.format || format, item.text || item);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
window.clipboardData.setData(format, text);
|
|
||||||
}
|
|
||||||
options.onCopy && options.onCopy(window.clipboardData);
|
|
||||||
success = true;
|
|
||||||
} catch (err) {
|
|
||||||
debug && console.error('unable to copy using clipboardData: ', err);
|
|
||||||
debug && console.error('falling back to prompt');
|
|
||||||
message = format('message' in options ? options.message : defaultMessage);
|
|
||||||
window.prompt(message, text);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (selection) {
|
|
||||||
if (typeof selection.removeRange == 'function') {
|
|
||||||
selection.removeRange(range);
|
|
||||||
} else {
|
|
||||||
selection.removeAllRanges();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mark) {
|
if (!successful) {
|
||||||
document.body.removeChild(mark);
|
throw new Error('copy command was unsuccessful');
|
||||||
}
|
|
||||||
reselectPrevious();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,6 @@
|
||||||
import _copy from './copy-to-clipboard';
|
import _copy from './copy-to-clipboard';
|
||||||
import { Toast } from '@douyinfe/semi-ui';
|
import { Toast } from '@douyinfe/semi-ui';
|
||||||
|
|
||||||
interface Options {
|
export function copy(text: string | { text: string; format: string }[]) {
|
||||||
debug?: boolean;
|
return _copy(text, () => Toast.success('复制成功'));
|
||||||
message?: string;
|
|
||||||
format?: string; // MIME type
|
|
||||||
onCopy?: (clipboardData: object) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function copy(text: string | { text: string; format: string }[], options?: Options) {
|
|
||||||
options = options || {};
|
|
||||||
options.onCopy = options.onCopy || (() => Toast.success(options.message || '复制成功'));
|
|
||||||
options.format = options.format || 'text/plain';
|
|
||||||
return _copy(text, options);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue