Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

57
LINES

< > BotCompany Repo | #1031627 // HTMLAceEditor [better version of hAceEditor that can put multiple editors on one page]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5239L/30K).

1  
// requires JQuery
2  
sclass HTMLAceEditor implements Htmlable {
3  
  S text;
4  
  S name = "text";
5  
  S id;
6  
  Map divParams = litmap(style := "width: 80ch; height: 20em");
7  
  S onKeyDown;
8  
  
9  
  *() {}
10  
  *(S *text) {}
11  
  
12  
  // only needed once (call on any instance)
13  
  public S headStuff() {
14  
    ret hscriptsrc("https://botcompany.de/ace-builds/src-noconflict/ace.js")
15  
      + hscriptsrc("https://botcompany.de/ace-builds/src-noconflict/ext-language_tools.js");
16  
  }
17  
  
18  
  // for each editor
19  
  public S html() {
20  
    id = "ace_" + name;
21  
  
22  
    ret div(htmlEncode2(text), params_stylePlus("display: none", paramsPlus(mapToParams(divParams), +id)))
23  
      + hhiddenWithIDAndName(name)
24  
      + hscript(replaceDollarVars([[ (function() {
25  
        ace.require("ace/ext/language_tools");
26  
        var editor = ace.edit($id);
27  
        editor.setTheme("ace/theme/ambience");
28  
        editor.getSession().setTabSize(2);
29  
        editor.getSession().setUseSoftTabs(true);
30  
        editor.getSession().setUseWrapMode(true);
31  
        document.getElementById($id).style.fontSize='15px';
32  
        editor.setOptions({
33  
          enableBasicAutocompletion: true
34  
        });
35  
      
36  
        var div = $("#" + $id);
37  
        var hiddenVal = document.getElementById($name);
38  
        function updateHidden() {
39  
          div.trigger('input'); // for auto-expand
40  
          //hiddenVal.value = editor.getValue();
41  
          var newVal = editor.getValue();
42  
          if (hiddenVal.value != newVal)
43  
            $(hiddenVal).val(newVal).trigger('change');
44  
        }
45  
        updateHidden();
46  
      
47  
        editor.session.on('change', updateHidden);
48  
        $onKeyDown
49  
  
50  
        div.show();
51  
        //editor.focus();
52  
      })() ]], id := jsQuote(id), name := jsQuote(name),
53  
        onKeyDown := empty(onKeyDown) ? "" : "editor.textInput.getElement().onkeydown = " + onKeyDown + ";");
54  
  }
55  
  
56  
  S complete() { ret linesLL(headStuff(), html()); }
57  
}

Author comment

Began life as a copy of #1030234

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031627
Snippet name: HTMLAceEditor [better version of hAceEditor that can put multiple editors on one page]
Eternal ID of this version: #1031627/13
Text MD5: ae82dc1837feba86cbef70080b2ae566
Transpilation MD5: daa911624c575215f77aa7cda2f73ef6
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-04 03:51:51
Source code size: 1944 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 201 / 403
Version history: 12 change(s)
Referenced in: [show references]