0766acfbbf14be853fbd9f18cd240fae3d65bfdf
[redakcja.git] / src / redakcja / static / js / wiki / view_editor_source.js
1 (function($) {
2
3     class CodeMirrorPerspective extends $.wiki.Perspective {
4         constructor(options) {
5             var old_callback = options.callback;
6             options.callback = function(){
7                 var self = this;
8
9                 this.codemirror = CodeMirror.fromTextArea($(
10                     '#codemirror_placeholder').get(0), {
11                         mode: 'xml',
12                         lineWrapping: true,
13                         lineNumbers: true,
14                         readOnly: CurrentDocument.readonly || false,
15                         identUnit: 0,
16                     });
17
18                 $('#source-editor').keydown(function(event) {
19                     if(!event.altKey)
20                         return;
21
22                     var c = event.key;
23                     var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
24                     if(button.length == 0)
25                         return;
26                     button.get(0).click();
27                     event.preventDefault();
28                 });
29
30                 $('#source-editor .toolbar').toolbarize({
31                     actionContext: self.codemirror
32                 });
33
34                 // textarea is no longer needed
35                 $('#codemirror_placeholder').remove();
36                 old_callback.call(self);
37             }
38             super(options);
39         }
40
41         freezeState() {
42             this.config().position =  this.codemirror.getScrollInfo().top;
43         }
44
45         unfreezeState () {
46             this.codemirror.scrollTo(0, this.config().position || 0);
47         }
48
49         onEnter(success, failure) {
50             super.onEnter();
51
52             this.codemirror.setValue(this.doc.text);
53
54             this.unfreezeState(this._uistate);
55
56             if(success) success();
57         }
58
59         onExit(success, failure) {
60             this.freezeState();
61
62             super.onExit();
63             this.doc.setText(this.codemirror.getValue());
64
65             $.wiki.exitTab('#SearchPerspective');
66
67             if(success) success();
68         }
69     }
70
71     $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
72
73 })(jQuery);