button.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. (function($){
  2. $(function(){
  3. $('body').on('grav-editor-ready', function() {
  4. var Instance = Grav.default.Forms.Fields.EditorField.Instance;
  5. Instance.addButton({
  6. youtube: {
  7. identifier: 'youtube-video',
  8. title: 'YouTube Video',
  9. label: '<i class="fa fa-fw fa-youtube"></i>',
  10. modes: ['gfm', 'markdown'],
  11. action: function(_ref) {
  12. var codemirror = _ref.codemirror, button = _ref.button, textarea = _ref.textarea;
  13. button.on('click.editor.youtube',function() {
  14. var videoURL = prompt("Enter the YouTube Video URL. E.g. https://www.youtube.com/watch?v=vQ4qK36UenI");
  15. if (videoURL) {
  16. var text = '[plugin:youtube](' + videoURL + ')';
  17. //Add text to the editor
  18. var pos = codemirror.getDoc().getCursor(true);
  19. var posend = codemirror.getDoc().getCursor(false);
  20. for (var i=pos.line; i<(posend.line+1);i++) {
  21. codemirror.replaceRange(text+codemirror.getLine(i), { line: i, ch: 0 }, { line: i, ch: codemirror.getLine(i).length });
  22. }
  23. codemirror.setCursor({ line: posend.line, ch: codemirror.getLine(posend.line).length });
  24. codemirror.focus();
  25. }
  26. });
  27. }
  28. }
  29. });
  30. });
  31. });
  32. })(jQuery);