PasteHelper.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import accepts from 'attr-accept'
  2. import markdownListHelper from './MarkdownListHelper';
  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. markdownListHelper.pasteText(editor, event, text);
  19. // [TODO] add markdownTableHelper
  20. }
  21. // Firefox versions prior to 53 return a bogus MIME type for every file drag, so dragovers with
  22. /**
  23. * transplanted from react-dropzone
  24. * @see https://github.com/react-dropzone/react-dropzone/blob/master/src/utils/index.js
  25. *
  26. * @param {*} file
  27. * @param {*} accept
  28. */
  29. fileAccepted(file, accept) {
  30. return file.type === 'application/x-moz-file' || accepts(file, accept)
  31. }
  32. /**
  33. * transplanted from react-dropzone
  34. * @see https://github.com/react-dropzone/react-dropzone/blob/master/src/utils/index.js
  35. *
  36. * @param {*} file
  37. * @param {number} maxSize
  38. * @param {number} minSize
  39. */
  40. fileMatchSize(file, maxSize, minSize) {
  41. return file.size <= maxSize && file.size >= minSize
  42. }
  43. }
  44. // singleton pattern
  45. const instance = new PasteHelper();
  46. Object.freeze(instance);
  47. export default instance;