T3editor.ts 2.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

14
15
import './Element/CodeMirrorElement';
import DocumentService = require('TYPO3/CMS/Core/DocumentService');
16
17
18
19
20

/**
 * Module: TYPO3/CMS/T3editor/T3editor
 * Renders CodeMirror into FormEngine
 * @exports TYPO3/CMS/T3editor/T3editor
21
 * @deprecated since v11.1, will be removed in v12
22
23
24
25
26
27
28
29
30
 */
class T3editor {

  /**
   * @param {string} position
   * @param {string} label
   * @returns {HTMLElement}
   */
  public static createPanelNode(position: string, label: string): HTMLElement {
31
32
33
    const node = document.createElement('div');
    node.setAttribute('class', 'CodeMirror-panel CodeMirror-panel-' + position);
    node.setAttribute('id', 'panel-' + position);
34

35
36
37
38
39
40
    const span = document.createElement('span');
    span.textContent = label;

    node.appendChild(span);

    return node;
41
42
43
44
45
46
  }

  /**
   * The constructor, set the class properties default values
   */
  constructor() {
47
    console.warn('TYPO3/CMS/T3editor/T3editor has been marked as deprecated. Please use TYPO3/CMS/T3editor/Element/CodeMirrorElement instead.');
48
49
50
51
    this.initialize();
  }

  /**
52
   * Initialize the events
53
   */
54
  public initialize(): void {
55
    DocumentService.ready().then((): void => {
56
      this.observeEditorCandidates();
57
58
59
60
    });
  }

  /**
61
   * Initializes CodeMirror on available texteditors
62
   */
63
64
  public observeEditorCandidates(): void {
    document.querySelectorAll('textarea.t3editor').forEach((textarea: HTMLTextAreaElement): void => {
65
66
      if (textarea.parentElement.tagName.toLowerCase() === 'typo3-t3editor-codemirror') {
        return;
67
      }
68
69
70
71
72
73
74
75
      const editor = document.createElement('typo3-t3editor-codemirror');
      const config = JSON.parse(textarea.getAttribute('data-codemirror-config'));
      editor.setAttribute('mode', config.mode);
      editor.setAttribute('label', config.label);
      editor.setAttribute('addons', config.addons);
      editor.setAttribute('options', config.options);

      this.wrap(textarea, editor);
76
77
    });
  }
78
79
80
81
82

  private wrap(toWrap: HTMLElement, wrapper: HTMLElement) {
    toWrap.parentElement.insertBefore(wrapper, toWrap);
    wrapper.appendChild(toWrap);
  };
83
84
}

85
// create an instance and return it
86
export = new T3editor();