mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-07 18:03:48 +00:00
[copy link] improved 'copy link to the clipboard' (#4419)
* copy link to the clipboard, and make it shown as a link when pasted into the seafile markdown editor
This commit is contained in:
45
frontend/src/components/copy-to-clipboard.js
Normal file
45
frontend/src/components/copy-to-clipboard.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import deselectCurrent from 'toggle-selection';
|
||||||
|
|
||||||
|
function copy(text) {
|
||||||
|
let reselectPrevious, range, selection, mark, success = false;
|
||||||
|
try {
|
||||||
|
reselectPrevious = deselectCurrent();
|
||||||
|
|
||||||
|
range = document.createRange();
|
||||||
|
selection = document.getSelection();
|
||||||
|
|
||||||
|
// 'a' is for 'copy a link to seafile markdown editor'
|
||||||
|
mark = document.createElement('a');
|
||||||
|
mark.textContent = text;
|
||||||
|
|
||||||
|
document.body.appendChild(mark);
|
||||||
|
|
||||||
|
range.selectNode(mark);
|
||||||
|
selection.addRange(range);
|
||||||
|
|
||||||
|
const successful = document.execCommand('copy');
|
||||||
|
if (!successful) {
|
||||||
|
//console.log('copy command was unsuccessful');
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
} catch (err) {
|
||||||
|
// console.error('unable to copy using execCommand');
|
||||||
|
} finally {
|
||||||
|
if (selection) {
|
||||||
|
if (typeof selection.removeRange == 'function') {
|
||||||
|
selection.removeRange(range);
|
||||||
|
} else {
|
||||||
|
selection.removeAllRanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mark) {
|
||||||
|
document.body.removeChild(mark);
|
||||||
|
}
|
||||||
|
reselectPrevious();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default copy;
|
@@ -2,10 +2,11 @@ import React from 'react';
|
|||||||
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import toaster from '../toast';
|
import toaster from '../toast';
|
||||||
import copy from '@seafile/seafile-editor/dist/utils/copy-to-clipboard';
|
import copy from '../copy-to-clipboard';
|
||||||
import { gettext } from '../../utils/constants';
|
import { gettext } from '../../utils/constants';
|
||||||
import { seafileAPI } from '../../utils/seafile-api';
|
import { seafileAPI } from '../../utils/seafile-api';
|
||||||
import { Utils } from '../../utils/utils';
|
import { Utils } from '../../utils/utils';
|
||||||
|
|
||||||
import '../../css/internal-link.css';
|
import '../../css/internal-link.css';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { Button } from 'reactstrap';
|
import { Button } from 'reactstrap';
|
||||||
import toaster from '../toast';
|
import toaster from '../toast';
|
||||||
import copy from '@seafile/seafile-editor/dist/utils/copy-to-clipboard';
|
import copy from '../copy-to-clipboard';
|
||||||
import { gettext } from '../../utils/constants';
|
import { gettext } from '../../utils/constants';
|
||||||
import { seafileAPI } from '../../utils/seafile-api';
|
import { seafileAPI } from '../../utils/seafile-api';
|
||||||
import { Utils } from '../../utils/utils';
|
import { Utils } from '../../utils/utils';
|
||||||
|
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
|
|||||||
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
||||||
import { gettext } from '../../utils/constants';
|
import { gettext } from '../../utils/constants';
|
||||||
import toaster from '../toast';
|
import toaster from '../toast';
|
||||||
import copy from '@seafile/seafile-editor/dist//utils/copy-to-clipboard';
|
import copy from '../copy-to-clipboard';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
toggle: PropTypes.func.isRequired,
|
toggle: PropTypes.func.isRequired,
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
|
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
|
||||||
import copy from '@seafile/seafile-editor/dist//utils/copy-to-clipboard';
|
import copy from '../copy-to-clipboard';
|
||||||
import { gettext } from '../../utils/constants';
|
import { gettext } from '../../utils/constants';
|
||||||
import toaster from '../../components/toast';
|
import toaster from '../../components/toast';
|
||||||
|
|
||||||
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
||||||
import toaster from '../toast';
|
import toaster from '../toast';
|
||||||
import copy from '@seafile/seafile-editor/dist/utils/copy-to-clipboard';
|
import copy from '../copy-to-clipboard';
|
||||||
import { gettext } from '../../utils/constants';
|
import { gettext } from '../../utils/constants';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
|
Reference in New Issue
Block a user