Skip to content

Commit

Permalink
Recover bs3 codeview.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Sep 9, 2015
1 parent 62f5863 commit c20ca04
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
66 changes: 22 additions & 44 deletions src/js/bs3/module/Codeview.js
@@ -1,6 +1,6 @@
define([
'summernote/core/agent',
'summernote/core/dom'
'summernote/base/core/agent',
'summernote/base/core/dom'
], function (agent, dom) {

var CodeMirror;
Expand All @@ -17,61 +17,49 @@ define([
/**
* @class Codeview
*/
var Codeview = function (handler) {
var Codeview = function (summernote) {
var self = this;
var ui = $.summernote.ui;

this.sync = function (layoutInfo) {
var isCodeview = handler.invoke('codeview.isActivated', layoutInfo);
var $editor = summernote.layoutInfo.editor;
var $editable = summernote.layoutInfo.editable;
var $codable = summernote.layoutInfo.codable;
var options = summernote.options;

this.sync = function () {
var isCodeview = this.isActivated();
if (isCodeview && agent.hasCodeMirror) {
layoutInfo.codable().data('cmEditor').save();
$codable.data('cmEditor').save();
}
};

/**
* @param {Object} layoutInfo
* @return {Boolean}
*/
this.isActivated = function (layoutInfo) {
var $editor = layoutInfo.editor();
this.isActivated = function () {
return $editor.hasClass('codeview');
};

/**
* toggle codeview
*
* @param {Object} layoutInfo
*/
this.toggle = function (layoutInfo) {
if (this.isActivated(layoutInfo)) {
this.deactivate(layoutInfo);
this.toggle = function () {
if (this.isActivated()) {
this.deactivate();
} else {
this.activate(layoutInfo);
this.activate();
}
};

/**
* activate code view
*
* @param {Object} layoutInfo
*/
this.activate = function (layoutInfo) {
var $editor = layoutInfo.editor(),
$toolbar = layoutInfo.toolbar(),
$editable = layoutInfo.editable(),
$codable = layoutInfo.codable(),
$popover = layoutInfo.popover(),
$handle = layoutInfo.handle();

var options = $editor.data('options');

this.activate = function () {
$codable.val(dom.html($editable, options.prettifyHtml));
$codable.height($editable.height());

handler.invoke('toolbar.updateCodeview', $toolbar, true);
handler.invoke('popover.hide', $popover);
handler.invoke('handle.hide', $handle);

summernote.invoke('toolbar.updateCodeview', [true]);
$editor.addClass('codeview');

$codable.focus();

// activate CodeMirror as codable
Expand Down Expand Up @@ -99,14 +87,6 @@ define([
* @param {Object} layoutInfo
*/
this.deactivate = function (layoutInfo) {
var $holder = layoutInfo.holder(),
$editor = layoutInfo.editor(),
$toolbar = layoutInfo.toolbar(),
$editable = layoutInfo.editable(),
$codable = layoutInfo.codable();

var options = $editor.data('options');

// deactivate CodeMirror as codable
if (agent.hasCodeMirror) {
var cmEditor = $codable.data('cmEditor');
Expand All @@ -122,14 +102,12 @@ define([
$editor.removeClass('codeview');

if (isChange) {
handler.bindCustomEvent(
$holder, $editable.data('callbacks'), 'change'
)($editable.html(), $editable);
summernote.triggerEvent('change', [$editable.html(), $editable]);
}

$editable.focus();

handler.invoke('toolbar.updateCodeview', $toolbar, false);
summernote.invoke('toolbar.updateCodeview', [false]);
};
};

Expand Down
8 changes: 7 additions & 1 deletion src/js/bs3/module/Toolbar.js
Expand Up @@ -124,6 +124,10 @@ define([
$toolbar.find('.btn-fullscreen').toggleClass('active', isFullscreen);
};

this.updateCodeview = function (isCodeview) {
$toolbar.find('.btn-codeview').toggleClass('active', isCodeview);
};

this.initialize = function () {
$note.on('summernote.keyup summernote.mouseup summernote.change', function () {
self.updateCurrentStyle();
Expand Down Expand Up @@ -402,7 +406,9 @@ define([
click: this.createInvokeHandler('fullscreen.toggle')
}),
ui.button({
contents: '<i class="fa fa-code"/>'
className: 'btn-codeview',
contents: '<i class="fa fa-code"/>',
click: this.createInvokeHandler('codeview.toggle')
})
]).render());

Expand Down
8 changes: 5 additions & 3 deletions src/js/bs3/settings.js
Expand Up @@ -5,8 +5,9 @@ define([
'summernote/bs3/module/Statusbar',
'summernote/bs3/module/LinkDialog',
'summernote/bs3/module/ImageDialog',
'summernote/bs3/module/Fullscreen'
], function (ui, Editor, Toolbar, Statusbar, LinkDialog, ImageDialog, Fullscreen) {
'summernote/bs3/module/Fullscreen',
'summernote/bs3/module/Codeview'
], function (ui, Editor, Toolbar, Statusbar, LinkDialog, ImageDialog, Fullscreen, Codeview) {
var settings = {
version: '@VERSION',
ui: ui,
Expand All @@ -18,7 +19,8 @@ define([
'statusbar': Statusbar,
'linkDialog': LinkDialog,
'imageDialog': ImageDialog,
'fullscreen': Fullscreen
'fullscreen': Fullscreen,
'codeview': Codeview
},

width: null,
Expand Down
10 changes: 5 additions & 5 deletions src/js/bs3/ui.js
@@ -1,11 +1,11 @@
define([
'summernote/base/renderer'
], function (renderer) {
var editor = renderer.create('<div class="note-editor panel panel-default">');
var toolbar = renderer.create('<div class="note-toolbar panel-heading">');
var editingArea = renderer.create('<div class="note-editing-area">');
var codable = renderer.create('<div class="note-codable">');
var editable = renderer.create('<div class="note-editable panel-body" contentEditable="true">');
var editor = renderer.create('<div class="note-editor panel panel-default"/>');
var toolbar = renderer.create('<div class="note-toolbar panel-heading"/>');
var editingArea = renderer.create('<div class="note-editing-area"/>');
var codable = renderer.create('<textarea class="note-codable"/>');
var editable = renderer.create('<div class="note-editable panel-body" contentEditable="true"/>');
var statusbar = renderer.create([
'<div class="note-statusbar">',
' <div class="note-resizebar">',
Expand Down

0 comments on commit c20ca04

Please sign in to comment.