(delta: Delta)
| 26 | } |
| 27 | |
| 28 | applyDelta(delta: Delta): Delta { |
| 29 | this.scroll.update(); |
| 30 | let scrollLength = this.scroll.length(); |
| 31 | this.scroll.batchStart(); |
| 32 | const normalizedDelta = normalizeDelta(delta); |
| 33 | const deleteDelta = new Delta(); |
| 34 | const normalizedOps = splitOpLines(normalizedDelta.ops.slice()); |
| 35 | normalizedOps.reduce((index, op) => { |
| 36 | const length = Op.length(op); |
| 37 | let attributes = op.attributes || {}; |
| 38 | let isImplicitNewlinePrepended = false; |
| 39 | let isImplicitNewlineAppended = false; |
| 40 | if (op.insert != null) { |
| 41 | deleteDelta.retain(length); |
| 42 | if (typeof op.insert === 'string') { |
| 43 | const text = op.insert; |
| 44 | isImplicitNewlineAppended = |
| 45 | !text.endsWith('\n') && |
| 46 | (scrollLength <= index || |
| 47 | !!this.scroll.descendant(BlockEmbed, index)[0]); |
| 48 | this.scroll.insertAt(index, text); |
| 49 | const [line, offset] = this.scroll.line(index); |
| 50 | let formats = merge({}, bubbleFormats(line)); |
| 51 | if (line instanceof Block) { |
| 52 | const [leaf] = line.descendant(LeafBlot, offset); |
| 53 | if (leaf) { |
| 54 | formats = merge(formats, bubbleFormats(leaf)); |
| 55 | } |
| 56 | } |
| 57 | attributes = AttributeMap.diff(formats, attributes) || {}; |
| 58 | } else if (typeof op.insert === 'object') { |
| 59 | const key = Object.keys(op.insert)[0]; // There should only be one key |
| 60 | if (key == null) return index; |
| 61 | const isInlineEmbed = this.scroll.query(key, Scope.INLINE) != null; |
| 62 | if (isInlineEmbed) { |
| 63 | if ( |
| 64 | scrollLength <= index || |
| 65 | !!this.scroll.descendant(BlockEmbed, index)[0] |
| 66 | ) { |
| 67 | isImplicitNewlineAppended = true; |
| 68 | } |
| 69 | } else if (index > 0) { |
| 70 | const [leaf, offset] = this.scroll.descendant(LeafBlot, index - 1); |
| 71 | if (leaf instanceof TextBlot) { |
| 72 | const text = leaf.value(); |
| 73 | if (text[offset] !== '\n') { |
| 74 | isImplicitNewlinePrepended = true; |
| 75 | } |
| 76 | } else if ( |
| 77 | leaf instanceof EmbedBlot && |
| 78 | leaf.statics.scope === Scope.INLINE_BLOT |
| 79 | ) { |
| 80 | isImplicitNewlinePrepended = true; |
| 81 | } |
| 82 | } |
| 83 | this.scroll.insertAt(index, key, op.insert[key]); |
| 84 | |
| 85 | if (isInlineEmbed) { |
no test coverage detected