PasteHelper.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import accepts from 'attr-accept';
  2. import markdownListUtil from './MarkdownListUtil';
  3. class PasteHelper {
  4. constructor() {
  5. this.pasteText = this.pasteText.bind(this);
  6. }
  7. /**
  8. * paste text
  9. * @param {any} editor An editor instance of CodeMirror
  10. * @param {any} event
  11. */
  12. pasteText(editor, event) {
  13. // get data in clipboard
  14. const text = event.clipboardData.getData('text/plain');
  15. if (text.length == 0) {
  16. return;
  17. }
  18. markdownListUtil.pasteText(editor, event, text);
  19. }
  20. // Firefox versions prior to 53 return a bogus MIME type for every file drag, so dragovers with
  21. /**
  22. * transplanted from react-dropzone
  23. * @see https://github.com/react-dropzone/react-dropzone/blob/master/src/utils/index.js
  24. *
  25. * @param {*} file
  26. * @param {*} accept
  27. */
  28. fileAccepted(file, accept) {
  29. return file.type === 'application/x-moz-file' || accepts(file, accept);
  30. }
  31. /**
  32. * transplanted from react-dropzone
  33. * @see https://github.com/react-dropzone/react-dropzone/blob/master/src/utils/index.js
  34. *
  35. * @param {*} file
  36. * @param {number} maxSize
  37. * @param {number} minSize
  38. */
  39. fileMatchSize(file, maxSize, minSize) {
  40. return file.size <= maxSize && file.size >= minSize;
  41. }
  42. }
  43. // singleton pattern
  44. const instance = new PasteHelper();
  45. Object.freeze(instance);
  46. export default instance;