Home Reference Source

application/components/common/rich-editor2/plugins/soft-breaks.js

  1. export default function(options = {}) {
  2. const myOptions = Object.assign({}, {
  3. 'shift': false,
  4. 'inBlocks': []
  5. }, options);
  6.  
  7. const useShift = myOptions.shift;
  8.  
  9. return {
  10. onKeyDown(e, editor, next) {
  11.  
  12. const {start} = editor.value.selection;
  13. const {document} = editor.value;
  14.  
  15.  
  16. if (e.key != 'Enter') {
  17. return next();
  18. }
  19.  
  20. const closest = document.getClosestBlock(start.key);
  21. let inBlock = false;
  22.  
  23. if (myOptions.inBlocks.indexOf(closest.get('type')) !== -1) {
  24. inBlock = true;
  25. }
  26.  
  27. if (useShift && e.shiftKey === false && inBlock == false) {
  28. return next();
  29. }
  30.  
  31. // for some reason, if you don't insert a non-zero width character
  32. // the cursor moves back to the head of the block and the new line
  33. // isn't inserted
  34. editor.insertText("\n ")
  35. .moveBackward(1);
  36. }
  37. };
  38. }