|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {NodeType} from 'prosemirror-model'; |
| 4 | +import {EditorView} from 'prosemirror-view'; |
| 5 | + |
| 6 | +import { |
| 7 | + BaseTooltipNode, |
| 8 | + BaseTooltipPluginOptions, |
| 9 | + BaseTooltipPluginView, |
| 10 | + TooltipOnChangeCallback, |
| 11 | +} from '../BaseTooltip'; |
| 12 | + |
| 13 | +import {TooltipButton} from './TooltipButton'; |
| 14 | + |
| 15 | +type ButtonContent = ( |
| 16 | + view: EditorView, |
| 17 | + node: BaseTooltipNode, |
| 18 | + toggleEdit: () => void, |
| 19 | +) => React.ReactElement; |
| 20 | + |
| 21 | +type FormContent = ( |
| 22 | + view: EditorView, |
| 23 | + node: BaseTooltipNode, |
| 24 | + onChange?: TooltipOnChangeCallback, |
| 25 | +) => React.ReactElement; |
| 26 | + |
| 27 | +interface TooltipMenuOptions extends BaseTooltipPluginOptions { |
| 28 | + disableHideOnBlur?: boolean; |
| 29 | + buttonContent: ButtonContent; |
| 30 | + formContent: FormContent; |
| 31 | +} |
| 32 | + |
| 33 | +export class TooltipButtonPluginView extends BaseTooltipPluginView { |
| 34 | + private enableHideOnBlur?: boolean; |
| 35 | + private forceEdit = false; |
| 36 | + private buttonContent: ButtonContent; |
| 37 | + private formContent: FormContent; |
| 38 | + |
| 39 | + constructor(view: EditorView, options: TooltipMenuOptions) { |
| 40 | + super(view, options); |
| 41 | + |
| 42 | + this.enableHideOnBlur = options.disableHideOnBlur !== true; |
| 43 | + this.buttonContent = options.buttonContent; |
| 44 | + this.formContent = options.formContent; |
| 45 | + if (this.enableHideOnBlur) { |
| 46 | + (this.view.dom as HTMLElement).addEventListener('focus', this.onViewFocus); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + toggleEdit = () => { |
| 51 | + this.forceEdit = !this.forceEdit; |
| 52 | + this.render(); |
| 53 | + }; |
| 54 | + |
| 55 | + destroy(): void { |
| 56 | + super.destroy(); |
| 57 | + (this.view.dom as HTMLElement).removeEventListener('focus', this.onViewFocus); |
| 58 | + } |
| 59 | + |
| 60 | + protected renderContent(currentNode: BaseTooltipNode): React.ReactNode { |
| 61 | + return ( |
| 62 | + <> |
| 63 | + {this.forceEdit && this.formContent ? ( |
| 64 | + <div>{this.formContent(this.view, currentNode, this.changeAttrsCb)}</div> |
| 65 | + ) : ( |
| 66 | + <TooltipButton domRef={currentNode.dom} onOutsideClick={this.onOutsideClick}> |
| 67 | + {this.buttonContent(this.view, currentNode, this.toggleEdit)} |
| 68 | + </TooltipButton> |
| 69 | + )} |
| 70 | + </> |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + protected setCurrentNode(node: NodeType) { |
| 75 | + super.setCurrentNode(node); |
| 76 | + this.currentNode?.dom?.addEventListener('dblclick', this.toggleEdit); |
| 77 | + } |
| 78 | + |
| 79 | + protected updateTooltipView() { |
| 80 | + this.currentNode?.dom?.removeEventListener('dblclick', this.toggleEdit); |
| 81 | + super.updateTooltipView(); |
| 82 | + } |
| 83 | + |
| 84 | + private onViewFocus = () => { |
| 85 | + this.forceEdit = false; |
| 86 | + this.manualHidden = false; |
| 87 | + this.render(); |
| 88 | + }; |
| 89 | +} |
0 commit comments