import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import { Doc } from '../../../fields/Doc'; import { LinkManager } from '../../util/LinkManager'; import { DocumentView } from '../nodes/DocumentView'; import { LinkDocPreview } from '../nodes/LinkDocPreview'; import { LinkEditor } from './LinkEditor'; import './LinkMenu.scss'; import { LinkMenuGroup } from './LinkMenuGroup'; import React = require('react'); interface Props { docView: DocumentView; position?: { x?: number; y?: number }; itemHandler?: (doc: Doc) => void; clearLinkEditor: () => void; } /** * the outermost component for the link menu of a node that contains a list of its linked nodes */ @observer export class LinkMenu extends React.Component { _editorRef = React.createRef(); @observable _editingLink?: Doc; @observable _linkMenuRef = React.createRef(); @computed get position() { return this.props.position ?? (dv => ({ x: dv?.left || 0, y: (dv?.bottom || 0) + 15 }))(this.props.docView.getBounds()); } clear = action(() => { this.props.clearLinkEditor(); this._editingLink = undefined; }); componentDidMount() { document.addEventListener('pointerdown', this.onPointerDown, true); } componentWillUnmount() { document.removeEventListener('pointerdown', this.onPointerDown, true); } onPointerDown = action((e: PointerEvent) => { LinkDocPreview.Clear(); if (!this._linkMenuRef.current?.contains(e.target as any) && !this._editorRef.current?.contains(e.target as any)) { this.clear(); } }); /** * maps each link to a JSX element to be rendered * @param groups containing info of all of the links * @returns list of link JSX elements if there at least one linked element */ renderAllGroups = (groups: Map>): Array => { const linkItems = Array.from(groups.entries()).map(group => ( (this._editingLink = linkDoc))} /> )); return linkItems.length ? linkItems : this.props.position ? [<>] : [

No links have been created yet. Drag the linking button onto another document to create a link.

]; }; render() { const sourceDoc = this.props.docView.props.Document; return (
{this._editingLink ? (
(this._editingLink = undefined))} />
) : (
{this.renderAllGroups(LinkManager.Instance.getRelatedGroupedLinks(sourceDoc))}
)}
); } }