| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <script src="http://cdn.ckeditor.com/4.5.4/standard-all/ckeditor.js"></script> |
|---|
| 4 | |
|---|
| 5 | <script type="text/javascript"> |
|---|
| 6 | var pluginName = 'testWidget'; |
|---|
| 7 | |
|---|
| 8 | CKEDITOR.config.height = 600; |
|---|
| 9 | CKEDITOR.config.width = 'auto'; |
|---|
| 10 | CKEDITOR.config.extraPlugins = pluginName; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | function addWidgetPlugin() { |
|---|
| 14 | |
|---|
| 15 | CKEDITOR.plugins.add(pluginName, { |
|---|
| 16 | requires: 'widget', |
|---|
| 17 | startNode: null, |
|---|
| 18 | endNode: null, |
|---|
| 19 | |
|---|
| 20 | init: function(editor) { |
|---|
| 21 | editor.widgets.add(pluginName, { |
|---|
| 22 | button: pluginName, |
|---|
| 23 | |
|---|
| 24 | mask: true, |
|---|
| 25 | |
|---|
| 26 | upcast: function(element) { |
|---|
| 27 | return element.name == pluginName; |
|---|
| 28 | }, |
|---|
| 29 | |
|---|
| 30 | }); |
|---|
| 31 | |
|---|
| 32 | editor.addCommand('insert', { |
|---|
| 33 | exec: function() { |
|---|
| 34 | var p = editor.document.createElement("p"); |
|---|
| 35 | editor.insertElement(p); |
|---|
| 36 | var div = editor.document.createElement("div"); |
|---|
| 37 | p.append(div); |
|---|
| 38 | |
|---|
| 39 | widget = editor.widgets.initOn( div, pluginName ); |
|---|
| 40 | |
|---|
| 41 | widget.focus(); |
|---|
| 42 | } |
|---|
| 43 | }); |
|---|
| 44 | editor.ui.addButton( pluginName, { |
|---|
| 45 | label: pluginName, |
|---|
| 46 | command: 'insert', |
|---|
| 47 | }); |
|---|
| 48 | |
|---|
| 49 | var bookmark = 'bookmark'; |
|---|
| 50 | var that = this; |
|---|
| 51 | |
|---|
| 52 | editor.addCommand(bookmark, { |
|---|
| 53 | exec: function() { |
|---|
| 54 | |
|---|
| 55 | if (that.startNode) { |
|---|
| 56 | that.startNode.remove(); |
|---|
| 57 | that.endNode.remove(); |
|---|
| 58 | that.startNode = that.endNode = null; |
|---|
| 59 | console.log('bookmark removed:'); |
|---|
| 60 | console.log(editor.getData()); |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | var sel = editor.getSelection(); |
|---|
| 65 | editor.lockSelection(sel); |
|---|
| 66 | var range = sel.getRanges()[0]; |
|---|
| 67 | if (range) { |
|---|
| 68 | range.trim(); |
|---|
| 69 | var bookmark = range.createBookmark(true); |
|---|
| 70 | that.startNode = editor.document.getById(bookmark.startNode); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | that.endNode = editor.document.getById(bookmark.endNode); |
|---|
| 74 | |
|---|
| 75 | console.log('bookmark created:'); |
|---|
| 76 | console.log(editor.getData()); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | editor.unlockSelection(sel); |
|---|
| 80 | } |
|---|
| 81 | }); |
|---|
| 82 | editor.ui.addButton( bookmark, { |
|---|
| 83 | label: bookmark, |
|---|
| 84 | command: bookmark |
|---|
| 85 | }); |
|---|
| 86 | } |
|---|
| 87 | }); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | addWidgetPlugin(); |
|---|
| 91 | |
|---|
| 92 | var initSample = function() { |
|---|
| 93 | var editorElement = CKEDITOR.document.getById( 'editor' ); |
|---|
| 94 | CKEDITOR.replace( 'editor' ); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | </script> |
|---|
| 98 | </head> |
|---|
| 99 | <body> |
|---|
| 100 | <div id="editor"> |
|---|
| 101 | <script type="text/javascript"> |
|---|
| 102 | initSample(); |
|---|
| 103 | </script> |
|---|
| 104 | </body> |
|---|
| 105 | </html> |
|---|
| 106 | |
|---|