Blame view

public/assets/addons/fastflow/plugins/codemirror/src/edit/fromTextArea.js 1.89 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  import { CodeMirror } from "./CodeMirror"
  import { activeElt } from "../util/dom"
  import { off, on } from "../util/event"
  import { copyObj } from "../util/misc"
  
  export function fromTextArea(textarea, options) {
    options = options ? copyObj(options) : {}
    options.value = textarea.value
    if (!options.tabindex && textarea.tabIndex)
      options.tabindex = textarea.tabIndex
    if (!options.placeholder && textarea.placeholder)
      options.placeholder = textarea.placeholder
    // Set autofocus to true if this textarea is focused, or if it has
    // autofocus and no other element is focused.
    if (options.autofocus == null) {
      let hasFocus = activeElt()
      options.autofocus = hasFocus == textarea ||
        textarea.getAttribute("autofocus") != null && hasFocus == document.body
    }
  
    function save() {textarea.value = cm.getValue()}
  
    let realSubmit
    if (textarea.form) {
      on(textarea.form, "submit", save)
      // Deplorable hack to make the submit method do the right thing.
      if (!options.leaveSubmitMethodAlone) {
        let form = textarea.form
        realSubmit = form.submit
        try {
          let wrappedSubmit = form.submit = () => {
            save()
            form.submit = realSubmit
            form.submit()
            form.submit = wrappedSubmit
          }
        } catch(e) {}
      }
    }
  
    options.finishInit = cm => {
      cm.save = save
      cm.getTextArea = () => textarea
      cm.toTextArea = () => {
        cm.toTextArea = isNaN // Prevent this from being ran twice
        save()
        textarea.parentNode.removeChild(cm.getWrapperElement())
        textarea.style.display = ""
        if (textarea.form) {
          off(textarea.form, "submit", save)
          if (typeof textarea.form.submit == "function")
            textarea.form.submit = realSubmit
        }
      }
    }
  
    textarea.style.display = "none"
    let cm = CodeMirror(node => textarea.parentNode.insertBefore(node, textarea.nextSibling),
      options)
    return cm
  }