all files / ui/ Clipboard.js

92.96% Statements 66/71
62.16% Branches 23/37
92.31% Functions 12/13
92.96% Lines 66/71
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                      371× 371× 371×   371× 371× 371×   371×           371× 371×       12×           80×             367× 367× 367×                                                                                     70×   70× 70× 134×       70× 70×   70× 70× 70× 68×   70× 66×         70×         70×                   70×             70× 66×                                                                   66× 66× 66×                   66× 66× 66× 66× 66×                
import { platform, substanceGlobals } from '../util'
import { copySelection, documentHelpers } from '../model'
import ClipboardImporter from './ClipboardImporter'
import ClipboardExporter from './ClipboardExporter'
 
/**
  The Clipboard is a Component which should be rendered as a sibling component
  of one or multiple Surfaces.
 
  It uses the JSONImporter and JSONExporter for internal copy'n'pasting,
  i.e., within one window or between two instances with the same DocumentSchema.
 
  For inter-application copy'n'paste, the ClipboardImporter and ClipboardExporter is used.
 
  @internal
*/
class Clipboard {
 
  constructor(editorSession, config) {
    this.editorSession = editorSession
    let doc = editorSession.getDocument()
    let schema = doc.getSchema()
 
    let htmlConverters = []
    Eif (config.converterRegistry) {
      htmlConverters = config.converterRegistry.get('html').values() || []
    }
    let _config = {
      schema: schema,
      DocumentClass: doc.constructor,
      converters: htmlConverters
    }
 
    this.htmlImporter = new ClipboardImporter(_config)
    this.htmlExporter = new ClipboardExporter(_config)
  }
 
  dispose() {
    this.htmlImporter.dispose()
    // does not need to be disposed
    // this.htmlExporter.dispose()
  }
 
  getEditorSession() {
    return this.editorSession
  }
 
  /*
    Called by to enable clipboard handling on a given root element.
  */
  attach(el) {
    el.on('copy', this.onCopy, this)
    el.on('cut', this.onCut, this)
    el.on('paste', this.onPaste, this)
  }
 
  /*
    Called by to disable clipboard handling.
  */
  detach(el) {
    el.off(this)
  }
 
  /*
    Called when copy event fired.
 
    @param {Event} event
  */
  onCopy(event) {
    // console.log("Clipboard.onCopy", arguments);
    let clipboardData = this._copy()
    substanceGlobals._clipboardData = event.clipboardData
 
    Eif (event.clipboardData && clipboardData.doc) {
      event.preventDefault()
      // store as plain text and html
      event.clipboardData.setData('text/plain', clipboardData.text)
      // WORKAROUND: under IE and Edge it is not permitted to set 'text/html'
      Eif (!platform.isIE && !platform.isEdge) {
        event.clipboardData.setData('text/html', clipboardData.html)
      }
    }
  }
 
  /*
    Called when cut event fired.
 
    @param {Event} event
  */
  onCut(event) {
    // preventing default behavior to avoid that contenteditable
    // destroys our DOM
    event.preventDefault()
    // console.log("Clipboard.onCut", arguments);
    this.onCopy(event)
    let editorSession = this.getEditorSession()
    editorSession.transaction((tx)=>{
      tx.deleteSelection()
    })
  }
 
  /*
    Called when paste event fired.
 
    @param {Event} event
  */
  // Works on Safari/Chrome/FF
  onPaste(event) {
    let clipboardData = event.clipboardData
 
    let types = {}
    for (let i = 0; i < clipboardData.types.length; i++) {
      types[clipboardData.types[i]] = true
    }
    // console.log('onPaste(): received content types', types);
 
    event.preventDefault()
    event.stopPropagation()
 
    let plainText
    let html
    if (types['text/plain']) {
      plainText = clipboardData.getData('text/plain')
    }
    if (types['text/html']) {
      html = clipboardData.getData('text/html')
    }
 
    // HACK: to allow at least in app copy and paste under Edge (which blocks HTML)
    // we guess by comparing the old and new plain text
    Iif (platform.isEdge &&
        substanceGlobals.clipboardData &&
        substanceGlobals.clipboardData.text === plainText) {
      html = substanceGlobals.clipboardData.html
    } else {
      substanceGlobals.clipboardData = {
        text: plainText,
        html: html
      }
    }
 
    // console.log('onPaste(): html = ', html);
 
    // WORKAROUND: FF does not provide HTML coming in from other applications
    // so fall back to pasting plain text
    Iif (platform.isFF && !html) {
      this._pastePlainText(plainText)
      return
    }
 
    // if we have content given as HTML we let the importer assess the quality first
    // and fallback to plain text import if it's bad
    if (html) {
      Iif (!this._pasteHtml(html, plainText)) {
        this._pastePlainText(plainText)
      }
    } else {
      this._pastePlainText(plainText)
    }
  }
 
  /*
    Pastes a given plain text into the surface.
 
    @param {String} plainText plain text
  */
  _pastePlainText(plainText) {
    let editorSession = this.getEditorSession()
    editorSession.transaction(function(tx) {
      tx.paste(plainText)
    }, { action: 'paste' })
  }
 
  /*
    Copies selected content from document to clipboard.
  */
  _copy() {
    let editorSession = this.getEditorSession()
    let sel = editorSession.getSelection()
    let doc = editorSession.getDocument()
    let clipboardDoc = null
    let clipboardText = ""
    let clipboardHtml = ""
    Eif (!sel.isCollapsed()) {
      clipboardText = documentHelpers.getTextForSelection(doc, sel) || ""
      clipboardDoc = copySelection(doc, sel)
      clipboardHtml = this.htmlExporter.exportDocument(clipboardDoc)
    }
    return {
      doc: clipboardDoc,
      html: clipboardHtml,
      text: clipboardText
    }
  }
 
  /*
    Pastes a given parsed html document into the surface.
 
    @param {ui/DOMElement} docElement
    @param {String} text plain text representation used as a fallback
  */
  _pasteHtml(html, text) {
    let content = this.htmlImporter.importDocument(html)
    this.paste(content, text)
    return true
  }
 
  /*
    Takes a clipboard document and pastes it at the current
    cursor position.
 
    Used by PasteCommand
  */
  paste(doc, text) {
    let content = doc || text
    let editorSession = this.getEditorSession()
    Eif (content) {
      editorSession.transaction((tx) => {
        tx.paste(content)
      }, { action: 'paste' })
    }
  }
 
}
 
export default Clipboard