forked from swmitra/html-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementMarkupEditHandler.js
86 lines (71 loc) · 2.91 KB
/
ElementMarkupEditHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @author Swagatam Mitra
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, document, console, brackets, $, Mustache */
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit");
var Resizer = brackets.getModule("utils/Resizer");
var CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror");
var lastSelectedElement;
var initialContent = null;
var markupEditor = null;
$(document).on("element.selected","#html-design-editor",function(event,element){
lastSelectedElement = element;
initialContent = lastSelectedElement.innerHTML;
markupEditor.off("change",_synchUpdate);
markupEditor.getDoc().setValue(initialContent);
markupEditor.refresh();
markupEditor.on("change",_synchUpdate);
});
$(document).on("deselect.all","#html-design-editor",function(event){
$("#markup-editor-container").hide();
});
function _handleEdit(){
if(lastSelectedElement){
$("#markup-editor-container").show();
markupEditor.refresh();
markupEditor.focus();
}
}
function _synchUpdate(){
lastSelectedElement = $(lastSelectedElement).html(markupEditor.getDoc().getValue())[0];
}
$(document).on("targetdom-dblclick","#html-design-editor", function(event){
_handleEdit();
});
$(document).on("click","#markup-edit-confirm", function(event){
_acceptEdit();
});
$(document).on("click","#markup-edit-cancel", function(event){
_revertEdit();
});
function _acceptEdit(){
$("#markup-editor-container").hide();
initialContent = null;
$("#html-design-editor").trigger('select.element',[lastSelectedElement]);
$("#html-design-editor").trigger('html.element.updated');
}
function _revertEdit(){
lastSelectedElement = $(lastSelectedElement).html(initialContent)[0];
$("#markup-editor-container").hide();
initialContent = null;
$("#html-design-editor").trigger('select.element',[lastSelectedElement]);
}
AppInit.appReady(function () {
markupEditor = CodeMirror.fromTextArea($('#markup-data-input')[0],{
lineWrapping: true,
lineNumbers: true,
mode: "htmlmixed"
});
Resizer.makeResizable($("#markup-editor-container")[0]
, Resizer.DIRECTION_HORIZONTAL
, Resizer.POSITION_RIGHT
, 100, false, undefined, false);
Resizer.makeResizable($("#markup-editor-container")[0]
, Resizer.DIRECTION_VERTICAL
, Resizer.POSITION_BOTTOM
, 140, false, undefined, false);
});
});