[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/plg_editors_none/js/ -> joomla-editor-none.js (source)

   1  /**
   2   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  window.customElements.define('joomla-editor-none', class extends HTMLElement {
   6    constructor() {
   7      super(); // Properties
   8  
   9      this.editor = ''; // Bindings
  10  
  11      this.unregisterEditor = this.unregisterEditor.bind(this);
  12      this.registerEditor = this.registerEditor.bind(this);
  13      this.childrenChange = this.childrenChange.bind(this);
  14      this.getSelection = this.getSelection.bind(this); // Watch for children changes.
  15      // eslint-disable-next-line no-return-assign
  16  
  17      new MutationObserver(() => this.childrenChange()).observe(this, {
  18        childList: true
  19      });
  20    }
  21    /**
  22     * Lifecycle
  23     */
  24  
  25  
  26    connectedCallback() {
  27      // Note the mutation observer won't fire for initial contents,
  28      // so childrenChange is also called here.
  29      this.childrenChange();
  30    }
  31    /**
  32     * Lifecycle
  33     */
  34  
  35  
  36    disconnectedCallback() {
  37      this.unregisterEditor();
  38    }
  39    /**
  40     * Get the selected text
  41     */
  42  
  43  
  44    getSelection() {
  45      if (document.selection) {
  46        // IE support
  47        this.editor.focus();
  48        return document.selection.createRange();
  49      }
  50  
  51      if (this.editor.selectionStart || this.editor.selectionStart === 0) {
  52        // MOZILLA/NETSCAPE support
  53        return this.editor.value.substring(this.editor.selectionStart, this.editor.selectionEnd);
  54      }
  55  
  56      return this.editor.value;
  57    }
  58    /**
  59     * Register the editor
  60     */
  61  
  62  
  63    registerEditor() {
  64      if (!window.Joomla || !window.Joomla.editors || typeof window.Joomla.editors !== 'object') {
  65        throw new Error('The Joomla API is not correctly registered.');
  66      }
  67  
  68      window.Joomla.editors.instances[this.editor.id] = {
  69        id: () => this.editor.id,
  70        element: () => this.editor,
  71        // eslint-disable-next-line no-return-assign
  72        getValue: () => this.editor.value,
  73        // eslint-disable-next-line no-return-assign
  74        setValue: text => this.editor.value = text,
  75        // eslint-disable-next-line no-return-assign
  76        getSelection: () => this.getSelection(),
  77        // eslint-disable-next-line no-return-assign
  78        disable: disabled => {
  79          this.editor.disabled = disabled;
  80          this.editor.readOnly = disabled;
  81        },
  82        // eslint-disable-next-line no-return-assign
  83        replaceSelection: text => {
  84          if (this.editor.selectionStart || this.editor.selectionStart === 0) {
  85            this.editor.value = this.editor.value.substring(0, this.editor.selectionStart) + text + this.editor.value.substring(this.editor.selectionEnd, this.editor.value.length);
  86          } else {
  87            this.editor.value += text;
  88          }
  89        },
  90        onSave: () => {}
  91      };
  92    }
  93    /**
  94     * Remove the editor from the Joomla API
  95     */
  96  
  97  
  98    unregisterEditor() {
  99      if (this.editor) {
 100        delete window.Joomla.editors.instances[this.editor.id];
 101      }
 102    }
 103    /**
 104     * Called when element's child list changes
 105     */
 106  
 107  
 108    childrenChange() {
 109      // Ensure the first child is an input with a textarea type.
 110      if (this.firstElementChild && this.firstElementChild.tagName && this.firstElementChild.tagName.toLowerCase() === 'textarea' && this.firstElementChild.getAttribute('id')) {
 111        this.editor = this.firstElementChild;
 112        this.unregisterEditor();
 113        this.registerEditor();
 114      }
 115    }
 116  
 117  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer