| 19 | }; |
| 20 | |
| 21 | export class SelectHighlight extends Interaction implements IInteraction { |
| 22 | name = 'select-highlight'; |
| 23 | |
| 24 | private highlightMasks: SVGRectElement[] = []; |
| 25 | private combinedBoundsMask?: SVGRectElement; |
| 26 | |
| 27 | init(options: InteractionInitOptions) { |
| 28 | super.init(options); |
| 29 | const { emitter } = options; |
| 30 | emitter.on('selection:change', this.handleSelectionChanged); |
| 31 | emitter.on('selection:geometrychange', this.handleGeometryChanged); |
| 32 | emitter.on('history:change', this.handleHistoryChanged); |
| 33 | this.highlightSelection(this.interaction.getSelection()); |
| 34 | } |
| 35 | |
| 36 | destroy() { |
| 37 | this.clearMasks(); |
| 38 | const { emitter } = this; |
| 39 | emitter.off('selection:change', this.handleSelectionChanged); |
| 40 | emitter.off('selection:geometrychange', this.handleGeometryChanged); |
| 41 | emitter.off('history:change', this.handleHistoryChanged); |
| 42 | } |
| 43 | |
| 44 | private handleSelectionChanged = ({ next }: SelectionChangePayload) => { |
| 45 | this.highlightSelection(next); |
| 46 | }; |
| 47 | |
| 48 | private handleGeometryChanged = ({ |
| 49 | target, |
| 50 | }: SelectionGeometryChangePayload) => { |
| 51 | if (this.interaction.isSelected(target)) { |
| 52 | this.highlightSelection(this.interaction.getSelection()); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | private handleHistoryChanged = () => { |
| 57 | this.highlightSelection(this.interaction.getSelection()); |
| 58 | }; |
| 59 | |
| 60 | private highlightSelection(selection: Selection) { |
| 61 | if (selection.length === 1 && isEditableText(selection[0])) { |
| 62 | this.clearMasks(); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | this.drawElementMasks(selection); |
| 67 | this.drawCombinedBoundsMask(selection); |
| 68 | } |
| 69 | |
| 70 | private drawElementMasks(selection: Selection) { |
| 71 | let index = 0; |
| 72 | for (; index < selection.length; index++) { |
| 73 | const { x, y, width, height } = getElementViewportBounds( |
| 74 | this.editor.getDocument(), |
| 75 | selection[index], |
| 76 | ); |
| 77 | const attrs = { |
| 78 | x, |
nothing calls this directly
no test coverage detected