├── .gitignore ├── README.md ├── LICENSE ├── example └── index.html └── src └── summernote-ext-highlight.js /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # summernote-ext-highlight 2 | Based on [Google code-prettify](https://github.com/google/code-prettify) the [summernote](https://github.com/summernote/summernote) code highlighting plugin 3 | 4 | ## Setup 5 | * Include summernote project script 6 | * Include [Font Awesome](http://fontawesome.io/) 7 | * Include the script tag below in your document 8 | ```HTML 9 | 10 | ``` 11 | 12 | ## Usage 13 | * Configuration summernote toolbar 14 | ```javascript 15 | $('.summernote').summernote({ 16 | height: 200, 17 | tabsize: 2, 18 | // close prettify Html 19 | prettifyHtml:false, 20 | toolbar:[ 21 | // Add highlight plugin 22 | ['highlight', ['highlight']], 23 | ], 24 | lang:'zh-CN' 25 | }); 26 | ``` 27 | ## Contacts 28 | * Email: yanlong.hee#gmail.com 29 | * Twitter: https://twitter.com/YanlongHe 30 | * Website: https://notes.yanlong.me/ 31 | 32 | ## License 33 | summernote-ext-highlight may be freely distributed under the MIT license. 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 何延龙 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |1 check insert code button
49 |2 enter the code fragment to editor
50 |3 check preview button
51 | 55 |');
88 | $code.html(code);
89 | $code.addClass('language-' + select);
90 |
91 | var $pre = $('');
92 | $pre.html($code)
93 | $pre.addClass('prettyprint').addClass('linenums');
94 |
95 | return $pre[0];
96 | };
97 |
98 | this.showHighlightDialog = function (codeInfo) {
99 | return $.Deferred(function (deferred) {
100 | var $extHighlightCode = self.$dialog.find('.ext-highlight-code');
101 | var $extHighlightBtn = self.$dialog.find('.ext-highlight-btn');
102 | var $extHighlightSelect = self.$dialog.find('.ext-highlight-select');
103 |
104 | ui.onDialogShown(self.$dialog, function () {
105 |
106 | $extHighlightCode.val(codeInfo);
107 |
108 | $extHighlightCode.on('input', function () {
109 | ui.toggleBtn($extHighlightBtn, $extHighlightCode.val() != '');
110 | codeInfo = $extHighlightCode.val();
111 | });
112 |
113 | $extHighlightBtn.one('click', function (event) {
114 | event.preventDefault();
115 | //转义后的html
116 | $code = $extHighlightCode.val().replace(/[<>&"]/g,function(c){return {'<':'<','>':'>','&':'&','"':'"'}[c];});
117 | //插入的代码类型
118 | $type = $extHighlightSelect.val();
119 | deferred.resolve(self.createCodeNode($code, $type));
120 | });
121 | });
122 |
123 | ui.onDialogHidden(self.$dialog, function () {
124 | $extHighlightBtn.off('click');
125 | if (deferred.state() === 'pending') {
126 | deferred.reject();
127 | }
128 | });
129 |
130 | ui.showDialog(self.$dialog);
131 | });
132 | };
133 |
134 | this.getCodeInfo = function () {
135 | var text = context.invoke('editor.getSelectedText');
136 | return '';
137 | };
138 |
139 | this.show = function () {
140 | var codeInfo = self.getCodeInfo();
141 | context.invoke('editor.saveRange');
142 | this.showHighlightDialog(codeInfo).then(function (codeInfo) {
143 | self.$dialog.modal('hide');
144 | context.invoke('editor.restoreRange');
145 |
146 | if (codeInfo) {
147 | context.invoke('editor.insertNode', codeInfo);
148 | }
149 | });
150 | };
151 |
152 | //// This events will be attached when editor is initialized.
153 | //this.event = {
154 | // // This will be called after modules are initialized.
155 | // 'summernote.init': function (we, e) {
156 | // console.log('summernote initialized', we, e);
157 | // },
158 | // // This will be called when user releases a key on editable.
159 | // 'summernote.keyup': function (we, e) {
160 | // console.log('summernote keyup', we, e);
161 | // }
162 | //};
163 | //
164 | //// This method will be called when editor is initialized by $('..').summernote();
165 | //// You can create elements for plugin
166 | this.initialize = function () {
167 | var $container = options.dialogsInBody ? $(document.body) : $editor;
168 |
169 | var body = [
170 | ''
173 | ].join('');
174 |
175 | this.$dialog = ui.dialog({
176 | className: 'ext-highlight',
177 | title: 'Insert code',
178 | body: this.createDialog(),
179 | footer: body,
180 | //callback: function ($node) {
181 | // $node.find('.modal-body').css({
182 | // 'max-height': 300,
183 | // 'overflow': 'scroll'
184 | // });
185 | //}
186 | }).render().appendTo($container);
187 | };
188 |
189 | // This methods will be called when editor is destroyed by $('..').summernote('destroy');
190 | // You should remove elements on `initialize`.
191 | this.destroy = function () {
192 | ui.hideDialog(this.$dialog);
193 | this.$dialog.remove();
194 | };
195 | }
196 | });
197 | }));
198 |
--------------------------------------------------------------------------------