Repository: marktext/marktext
File: src/muya/lib/index.js (JavaScript)

Code before:
    this.clipboard.pasteAsPlainText()
  }

  copy (name) {
    this.clipboard.copy(name)
  }

  setOptions (options, needRender = false) {
    this._validateOptions(options)
    Object.assign(this.options, options)
    if (needRender) {
      this.contentState.render()
    }

    // Set quick insert hint visibility
    const hideQuickInsertHint = options['hideQuickInsertHint']
    if (typeof hideQuickInsertHint !== 'undefined') {
      const hasClass = this.container.classList.contains('ag-show-quick-insert-hint')
      if (hideQuickInsertHint && hasClass) {
        this.container.classList.remove('ag-show-quick-insert-hint')
      } else if (!hideQuickInsertHint && !hasClass) {
        this.container.classList.add('ag-show-quick-insert-hint')
      }
    }

    const editorLineWidth = options['editorLineWidth']
    if (editorLineWidth) {
      this.container.style.setProperty('--editorAreaWidth', `calc(100px + ${editorLineWidth})`)
    } else {
      this.container.style.removeProperty('--editorAreaWidth')
    }

    if (options.bulletListMarker) {
      this.contentState.turndownConfig.bulletListMarker = options.bulletListMarker
    }
  }

  destroy () {
    this.contentState.clear()
    this.quickInsert.destroy()
    this.codePicker.destroy()
    this.tablePicker.destroy()
    this.emojiPicker.destroy()
    this.imagePathPicker.destroy()
    this.eventCenter.detachAllDomEvents()
  }

  _validateOptions (options) {
    const editorLineWidth = options['editorLineWidth']
    if (!editorLineWidth || !/^[0-9]+(?:ch|px|%)$/.test(editorLineWidth)) {
      options.editorLineWidth = ''

Review comment (suggestion):
I think it's not a good idea to control the editor width in muya. Muya should be more general. The control width should be controlled by external users, that is, in the application using muya to control the edit width.

Code after:
  }

  copy (name) {
    this.clipboard.copy(name)
  }

  setOptions (options, needRender = false) {
    Object.assign(this.options, options)
    if (needRender) {
      this.contentState.render()
    }

    // Set quick insert hint visibility
    const hideQuickInsertHint = options['hideQuickInsertHint']
    if (typeof hideQuickInsertHint !== 'undefined') {
      const hasClass = this.container.classList.contains('ag-show-quick-insert-hint')
      if (hideQuickInsertHint && hasClass) {
        this.container.classList.remove('ag-show-quick-insert-hint')
      } else if (!hideQuickInsertHint && !hasClass) {
        this.container.classList.add('ag-show-quick-insert-hint')
      }
    }

    if (options.bulletListMarker) {
      this.contentState.turndownConfig.bulletListMarker = options.bulletListMarker
    }
  }

  destroy () {
    this.contentState.clear()
    this.quickInsert.destroy()
    this.codePicker.destroy()
    this.tablePicker.destroy()
    this.emojiPicker.destroy()
    this.imagePathPicker.destroy()
    this.eventCenter.detachAllDomEvents()
  }
}

/**
  * [ensureContainerDiv ensure container element is div]
  */
function getContainer (originContainer, options) {
  const { hideQuickInsertHint } = options
  const container = document.createElement('div')
  const rootDom = document.createElement('div')
  const attrs = originContainer.attributes
  // copy attrs from origin container to new div element
  Array.from(attrs).forEach(attr => {
    container.setAttribute(attr.name, attr.value)
  })