LogseqPortalShape.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import { TLBoxShape, TLBoxShapeProps, TLResizeInfo, validUUID } from '@tldraw/core'
  3. import { HTMLContainer, TLComponentProps, useApp } from '@tldraw/react'
  4. import Vec from '@tldraw/vec'
  5. import { makeObservable, runInAction } from 'mobx'
  6. import { observer } from 'mobx-react-lite'
  7. import * as React from 'react'
  8. import { TablerIcon } from '~components/icons'
  9. import { SwitchInput } from '~components/inputs/SwitchInput'
  10. import { useCameraMovingRef } from '~hooks/useCameraMoving'
  11. import type { Shape } from '~lib'
  12. import { LogseqContext, SearchResult } from '~lib/logseq-context'
  13. import { CustomStyleProps, withClampedStyles } from './style-props'
  14. const HEADER_HEIGHT = 40
  15. export interface LogseqPortalShapeProps extends TLBoxShapeProps, CustomStyleProps {
  16. type: 'logseq-portal'
  17. pageId: string // page name or UUID
  18. blockType?: 'P' | 'B'
  19. collapsed?: boolean
  20. compact?: boolean
  21. collapsedHeight?: number
  22. }
  23. interface LogseqQuickSearchProps {
  24. onChange: (id: string) => void
  25. }
  26. const LogseqTypeTag = ({
  27. type,
  28. active,
  29. }: {
  30. type: 'B' | 'P' | 'WP' | 'BS' | 'PS'
  31. active?: boolean
  32. }) => {
  33. const nameMapping = {
  34. B: 'block',
  35. P: 'page',
  36. WP: 'whiteboard',
  37. BS: 'block-search',
  38. PS: 'page-search',
  39. }
  40. return (
  41. <span className="tl-type-tag" data-active={active}>
  42. <i className={`tie tie-${nameMapping[type]}`} />
  43. </span>
  44. )
  45. }
  46. const LogseqPortalShapeHeader = observer(
  47. ({ type, children }: { type: 'P' | 'B'; children: React.ReactNode }) => {
  48. return (
  49. <div className="tl-logseq-portal-header">
  50. <LogseqTypeTag type={type} />
  51. {children}
  52. </div>
  53. )
  54. }
  55. )
  56. function escapeRegExp(text: string) {
  57. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
  58. }
  59. const highlightedJSX = (input: string, keyword: string) => {
  60. return (
  61. <span>
  62. {input
  63. .split(new RegExp(`(${escapeRegExp(keyword)})`, 'gi'))
  64. .map((part, index) => {
  65. if (index % 2 === 1) {
  66. return <mark className="tl-highlighted">{part}</mark>
  67. }
  68. return part
  69. })
  70. .map((frag, idx) => (
  71. <React.Fragment key={idx}>{frag}</React.Fragment>
  72. ))}
  73. </span>
  74. )
  75. }
  76. const useSearch = (q: string) => {
  77. const { handlers } = React.useContext(LogseqContext)
  78. const [results, setResults] = React.useState<SearchResult | null>(null)
  79. React.useEffect(() => {
  80. let canceled = false
  81. const searchHandler = handlers?.search
  82. if (q.length > 0 && searchHandler) {
  83. handlers.search(q).then(_results => {
  84. if (!canceled) {
  85. setResults(_results)
  86. }
  87. })
  88. } else {
  89. setResults(null)
  90. }
  91. return () => {
  92. canceled = true
  93. }
  94. }, [q, handlers?.search])
  95. return results
  96. }
  97. export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
  98. static id = 'logseq-portal'
  99. static defaultProps: LogseqPortalShapeProps = {
  100. id: 'logseq-portal',
  101. type: 'logseq-portal',
  102. parentId: 'page',
  103. point: [0, 0],
  104. size: [400, 50],
  105. // collapsedHeight is the height before collapsing
  106. collapsedHeight: 0,
  107. stroke: 'var(--ls-primary-text-color)',
  108. fill: 'var(--ls-secondary-background-color)',
  109. strokeWidth: 2,
  110. opacity: 1,
  111. pageId: '',
  112. collapsed: false,
  113. compact: false,
  114. }
  115. hideRotateHandle = true
  116. canChangeAspectRatio = true
  117. canFlip = true
  118. canEdit = true
  119. persist: ((replace?: boolean) => void) | null = null
  120. // For quick add shapes, we want to calculate the page height dynamically
  121. initialHeightCalculated = true
  122. getInnerHeight: (() => number) | null = null // will be overridden in the hook
  123. constructor(props = {} as Partial<LogseqPortalShapeProps>) {
  124. super(props)
  125. makeObservable(this)
  126. if (props.collapsed || props.compact) {
  127. Object.assign(this.canResize, [true, false])
  128. }
  129. if (props.size?.[1] === 0) {
  130. this.initialHeightCalculated = false
  131. }
  132. }
  133. static isPageOrBlock(id: string): 'P' | 'B' | false {
  134. const blockRefEg = '((62af02d0-0443-42e8-a284-946c162b0f89))'
  135. if (id) {
  136. return /^\(\(.*\)\)$/.test(id) && id.length === blockRefEg.length ? 'B' : 'P'
  137. }
  138. return false
  139. }
  140. useComponentSize<T extends HTMLElement>(ref: React.RefObject<T> | null, selector = '') {
  141. const [size, setSize] = React.useState<[number, number]>([0, 0])
  142. const app = useApp<Shape>()
  143. React.useEffect(() => {
  144. if (ref?.current) {
  145. const el = selector ? ref.current.querySelector<HTMLElement>(selector) : ref.current
  146. if (el) {
  147. const updateSize = () => {
  148. const { width, height } = el.getBoundingClientRect()
  149. const bound = Vec.div([width, height], app.viewport.camera.zoom) as [number, number]
  150. setSize(bound)
  151. return bound
  152. }
  153. updateSize()
  154. // Hacky, I know 🤨
  155. this.getInnerHeight = () => updateSize()[1] + 2 // 2 is a hack to compensate for the border
  156. const resizeObserver = new ResizeObserver(() => {
  157. updateSize()
  158. })
  159. resizeObserver.observe(el)
  160. return () => {
  161. resizeObserver.disconnect()
  162. }
  163. }
  164. }
  165. return () => {}
  166. }, [ref, selector])
  167. return size
  168. }
  169. ReactContextBar = observer(() => {
  170. const app = useApp<Shape>()
  171. return (
  172. <>
  173. {this.props.blockType !== 'B' && (
  174. <SwitchInput
  175. label="Collapsed"
  176. checked={this.props.collapsed}
  177. onCheckedChange={collapsing => {
  178. runInAction(() => {
  179. const originalHeight = this.props.size[1]
  180. this.canResize[1] = !collapsing
  181. this.update({
  182. collapsed: collapsing,
  183. size: [
  184. this.props.size[0],
  185. collapsing ? HEADER_HEIGHT : this.props.collapsedHeight,
  186. ],
  187. collapsedHeight: collapsing ? originalHeight : this.props.collapsedHeight,
  188. })
  189. app.persist()
  190. })
  191. }}
  192. />
  193. )}
  194. {this.props.blockType === 'B' && (
  195. <SwitchInput
  196. label="Compact"
  197. checked={this.props.compact}
  198. onCheckedChange={compact => {
  199. runInAction(() => {
  200. this.update({ compact })
  201. this.canResize[1] = !compact
  202. if (!compact) {
  203. // this will also persist the state, so we can skip persist call
  204. this.autoResizeHeight()
  205. } else {
  206. app.persist()
  207. }
  208. })
  209. }}
  210. />
  211. )}
  212. </>
  213. )
  214. })
  215. shouldAutoResizeHeight() {
  216. return this.props.blockType === 'B' && this.props.compact
  217. }
  218. getHeaderHeight() {
  219. return this.props.compact ? 0 : HEADER_HEIGHT
  220. }
  221. getAutoResizeHeight() {
  222. if (this.getInnerHeight) {
  223. return this.getHeaderHeight() + this.getInnerHeight()
  224. }
  225. return null
  226. }
  227. autoResizeHeight(replace: boolean = false) {
  228. setTimeout(() => {
  229. const height = this.getAutoResizeHeight()
  230. if (height !== null) {
  231. this.update({
  232. size: [this.props.size[0], height],
  233. })
  234. this.persist?.(replace)
  235. this.initialHeightCalculated = true
  236. }
  237. })
  238. }
  239. onResize = (initialProps: any, info: TLResizeInfo): this => {
  240. const {
  241. bounds,
  242. rotation,
  243. scale: [scaleX, scaleY],
  244. } = info
  245. const nextScale = [...this.scale]
  246. if (scaleX < 0) nextScale[0] *= -1
  247. if (scaleY < 0) nextScale[1] *= -1
  248. let height = bounds.height
  249. if (this.shouldAutoResizeHeight()) {
  250. height = this.getAutoResizeHeight() ?? height
  251. }
  252. return this.update({
  253. point: [bounds.minX, bounds.minY],
  254. size: [Math.max(1, bounds.width), Math.max(1, height)],
  255. scale: nextScale,
  256. rotation,
  257. })
  258. }
  259. LogseqQuickSearch = observer(({ onChange }: LogseqQuickSearchProps) => {
  260. const [q, setQ] = React.useState('')
  261. const rInput = React.useRef<HTMLInputElement>(null)
  262. const { handlers, renderers } = React.useContext(LogseqContext)
  263. const app = useApp<Shape>()
  264. const finishCreating = React.useCallback((id: string) => {
  265. onChange(id)
  266. rInput.current?.blur()
  267. }, [])
  268. const onAddBlock = React.useCallback((content: string) => {
  269. const uuid = handlers?.addNewBlock(content)
  270. if (uuid) {
  271. finishCreating(uuid)
  272. // wait until the editor is mounted
  273. setTimeout(() => {
  274. // @ts-expect-error ???
  275. const logseqApi = window.logseq?.api as any
  276. if (logseqApi) {
  277. app.setEditingShape(this)
  278. logseqApi.edit_block(uuid)
  279. }
  280. })
  281. }
  282. }, [])
  283. const optionsWrapperRef = React.useRef<HTMLDivElement>(null)
  284. const [focusedOptionIdx, setFocusedOptionIdx] = React.useState<number>(0)
  285. const searchResult = useSearch(q)
  286. const [prefixIcon, setPrefixIcon] = React.useState<string>('circle-plus')
  287. const [searchFilter, setSearchFilter] = React.useState<'B' | 'P' | null>(null)
  288. React.useEffect(() => {
  289. // autofocus seems not to be working
  290. setTimeout(() => {
  291. rInput.current?.focus()
  292. })
  293. }, [searchFilter])
  294. type Option = {
  295. actionIcon: 'search' | 'circle-plus'
  296. onChosen: () => void
  297. element: React.ReactNode
  298. }
  299. const options: Option[] = React.useMemo(() => {
  300. const options: Option[] = []
  301. const Breadcrumb = renderers?.Breadcrumb
  302. if (!Breadcrumb || !handlers) {
  303. return []
  304. }
  305. // New block option
  306. options.push({
  307. actionIcon: 'circle-plus',
  308. onChosen: () => {
  309. onAddBlock(q)
  310. },
  311. element: (
  312. <div className="tl-quick-search-option-row">
  313. <LogseqTypeTag active type="B" />
  314. {q.length > 0 ? (
  315. <>
  316. <strong>New whiteboard block:</strong>
  317. {q}
  318. </>
  319. ) : (
  320. <strong>New whiteboard block</strong>
  321. )}
  322. </div>
  323. ),
  324. })
  325. // New page option
  326. if (searchResult?.pages?.length === 0 && q) {
  327. options.push({
  328. actionIcon: 'circle-plus',
  329. onChosen: () => {
  330. finishCreating(q)
  331. },
  332. element: (
  333. <div className="tl-quick-search-option-row">
  334. <LogseqTypeTag active type="P" />
  335. <strong>New page:</strong>
  336. {q}
  337. </div>
  338. ),
  339. })
  340. }
  341. // search filters
  342. if (q.length === 0 && searchFilter === null) {
  343. options.push(
  344. {
  345. actionIcon: 'search',
  346. onChosen: () => {
  347. setSearchFilter('B')
  348. },
  349. element: (
  350. <div className="tl-quick-search-option-row">
  351. <LogseqTypeTag type="BS" />
  352. Search only blocks
  353. </div>
  354. ),
  355. },
  356. {
  357. actionIcon: 'search',
  358. onChosen: () => {
  359. setSearchFilter('P')
  360. },
  361. element: (
  362. <div className="tl-quick-search-option-row">
  363. <LogseqTypeTag type="PS" />
  364. Search only pages
  365. </div>
  366. ),
  367. }
  368. )
  369. }
  370. // Page results
  371. if ((!searchFilter || searchFilter === 'P') && searchResult && searchResult.pages) {
  372. options.push(
  373. ...searchResult.pages.map(page => {
  374. return {
  375. actionIcon: 'search' as 'search',
  376. onChosen: () => {
  377. finishCreating(page)
  378. },
  379. element: (
  380. <div className="tl-quick-search-option-row">
  381. <LogseqTypeTag type={handlers.isWhiteboardPage(page) ? 'WP' : 'P'} />
  382. {highlightedJSX(page, q)}
  383. </div>
  384. ),
  385. }
  386. })
  387. )
  388. }
  389. // Block results
  390. if ((!searchFilter || searchFilter === 'B') && searchResult && searchResult.blocks) {
  391. options.push(
  392. ...searchResult.blocks
  393. .filter(block => block.content && block.uuid)
  394. .map(({ content, uuid }) => {
  395. const block = handlers.queryBlockByUUID(uuid)
  396. return {
  397. actionIcon: 'search' as 'search',
  398. onChosen: () => {
  399. if (block) {
  400. finishCreating(uuid)
  401. }
  402. },
  403. element: block ? (
  404. <>
  405. <div className="tl-quick-search-option-row">
  406. <LogseqTypeTag type="B" />
  407. <div className="tl-quick-search-option-breadcrumb">
  408. <Breadcrumb blockId={uuid} />
  409. </div>
  410. </div>
  411. <div className="tl-quick-search-option-row">
  412. <div className="tl-quick-search-option-placeholder" />
  413. {highlightedJSX(content, q)}
  414. </div>
  415. </>
  416. ) : (
  417. <div className="tl-quick-search-option-row">
  418. Cache is outdated. Please click the 'Re-index' button in the graph's dropdown
  419. menu.
  420. </div>
  421. ),
  422. }
  423. })
  424. )
  425. }
  426. return options
  427. }, [q, searchFilter, searchResult, renderers?.Breadcrumb, handlers])
  428. React.useEffect(() => {
  429. const keydownListener = (e: KeyboardEvent) => {
  430. let newIndex = focusedOptionIdx
  431. if (e.key === 'ArrowDown') {
  432. newIndex = Math.min(options.length - 1, focusedOptionIdx + 1)
  433. } else if (e.key === 'ArrowUp') {
  434. newIndex = Math.max(0, focusedOptionIdx - 1)
  435. } else if (e.key === 'Enter') {
  436. options[focusedOptionIdx]?.onChosen()
  437. e.stopPropagation()
  438. e.preventDefault()
  439. } else if (e.key === 'Backspace' && q.length === 0) {
  440. setSearchFilter(null)
  441. }
  442. if (newIndex !== focusedOptionIdx) {
  443. const option = options[newIndex]
  444. setFocusedOptionIdx(newIndex)
  445. setPrefixIcon(option.actionIcon)
  446. e.stopPropagation()
  447. e.preventDefault()
  448. const optionElement = optionsWrapperRef.current?.querySelector(
  449. '.tl-quick-search-option:nth-child(' + (newIndex + 1) + ')'
  450. )
  451. if (optionElement) {
  452. // @ts-expect-error we are using scrollIntoViewIfNeeded, which is not in standards
  453. optionElement?.scrollIntoViewIfNeeded(false)
  454. }
  455. }
  456. }
  457. document.addEventListener('keydown', keydownListener, true)
  458. return () => {
  459. document.removeEventListener('keydown', keydownListener, true)
  460. }
  461. }, [options, focusedOptionIdx, q])
  462. return (
  463. <div className="tl-quick-search">
  464. <div className="tl-quick-search-indicator">
  465. <TablerIcon name={prefixIcon} className="tl-quick-search-icon" />
  466. </div>
  467. <div className="tl-quick-search-input-container">
  468. {searchFilter && (
  469. <div className="tl-quick-search-input-filter">
  470. <LogseqTypeTag type={searchFilter} />
  471. {searchFilter === 'B' ? 'Search blocks' : 'Search pages'}
  472. <div
  473. className="tl-quick-search-input-filter-remove"
  474. onClick={() => setSearchFilter(null)}
  475. >
  476. <TablerIcon name="x" />
  477. </div>
  478. </div>
  479. )}
  480. <div className="tl-quick-search-input-sizer" data-value={q}>
  481. <div className="tl-quick-search-input-hidden">{q}</div>
  482. <input
  483. ref={rInput}
  484. type="text"
  485. value={q}
  486. placeholder="Create or search your graph..."
  487. onChange={q => setQ(q.target.value)}
  488. onKeyDown={e => {
  489. if (e.key === 'Enter') {
  490. finishCreating(q)
  491. }
  492. }}
  493. className="tl-quick-search-input"
  494. />
  495. </div>
  496. </div>
  497. <div className="tl-quick-search-options" ref={optionsWrapperRef}>
  498. {options.map(({ actionIcon, onChosen, element }, index) => {
  499. return (
  500. <div
  501. key={index}
  502. data-focused={index === focusedOptionIdx}
  503. className="tl-quick-search-option"
  504. tabIndex={0}
  505. onMouseEnter={() => {
  506. setPrefixIcon(actionIcon)
  507. setFocusedOptionIdx(index)
  508. }}
  509. onClick={onChosen}
  510. >
  511. {element}
  512. </div>
  513. )
  514. })}
  515. </div>
  516. </div>
  517. )
  518. })
  519. PortalComponent = observer(({}: TLComponentProps) => {
  520. const {
  521. props: { pageId },
  522. } = this
  523. const { renderers } = React.useContext(LogseqContext)
  524. const app = useApp<Shape>()
  525. const cpRefContainer = React.useRef<HTMLDivElement>(null)
  526. const [, innerHeight] = this.useComponentSize(
  527. cpRefContainer,
  528. this.props.compact
  529. ? '.tl-logseq-cp-container > .single-block'
  530. : '.tl-logseq-cp-container > .page'
  531. )
  532. if (!renderers?.Page) {
  533. return null // not being correctly configured
  534. }
  535. const { Page, Block } = renderers
  536. React.useEffect(() => {
  537. if (this.shouldAutoResizeHeight()) {
  538. const newHeight = innerHeight + this.getHeaderHeight() + 2
  539. if (innerHeight && newHeight !== this.props.size[1]) {
  540. this.update({
  541. size: [this.props.size[0], newHeight],
  542. })
  543. app.persist(true)
  544. }
  545. }
  546. }, [innerHeight, this.props.compact])
  547. React.useEffect(() => {
  548. if (!this.initialHeightCalculated) {
  549. this.autoResizeHeight(true)
  550. }
  551. }, [this.initialHeightCalculated])
  552. return (
  553. <div
  554. ref={cpRefContainer}
  555. className="tl-logseq-cp-container"
  556. style={{
  557. overflow: this.props.compact ? 'visible' : 'auto',
  558. }}
  559. >
  560. {this.props.blockType === 'B' && this.props.compact ? (
  561. <Block blockId={pageId} />
  562. ) : (
  563. <Page pageName={pageId} />
  564. )}
  565. </div>
  566. )
  567. })
  568. ReactComponent = observer((componentProps: TLComponentProps) => {
  569. const { events, isErasing, isEditing, isBinding } = componentProps
  570. const {
  571. props: { opacity, pageId, stroke, fill },
  572. } = this
  573. const app = useApp<Shape>()
  574. const { renderers, handlers } = React.useContext(LogseqContext)
  575. this.persist = () => app.persist()
  576. const isMoving = useCameraMovingRef()
  577. const isSelected = app.selectedIds.has(this.id)
  578. const isCreating = app.isIn('logseq-portal.creating') && !pageId
  579. const tlEventsEnabled =
  580. (isMoving || (isSelected && !isEditing) || app.selectedTool.id !== 'select') && !isCreating
  581. const stop = React.useCallback(
  582. e => {
  583. if (!tlEventsEnabled) {
  584. // TODO: pinching inside Logseq Shape issue
  585. e.stopPropagation()
  586. }
  587. },
  588. [tlEventsEnabled]
  589. )
  590. // It is a bit weird to update shapes here. Is there a better place?
  591. React.useEffect(() => {
  592. if (this.props.collapsed && isEditing) {
  593. // Should temporarily disable collapsing
  594. this.update({
  595. size: [this.props.size[0], this.props.collapsedHeight],
  596. })
  597. return () => {
  598. this.update({
  599. size: [this.props.size[0], HEADER_HEIGHT],
  600. })
  601. }
  602. }
  603. return () => {
  604. // no-ops
  605. }
  606. }, [isEditing, this.props.collapsed])
  607. const onPageNameChanged = React.useCallback((id: string) => {
  608. this.initialHeightCalculated = false
  609. const blockType = validUUID(id) ? 'B' : 'P'
  610. this.update({
  611. pageId: id,
  612. size: [400, 320],
  613. blockType: blockType,
  614. compact: blockType === 'B',
  615. })
  616. app.selectTool('select')
  617. app.history.resume()
  618. app.history.persist()
  619. }, [])
  620. const PortalComponent = this.PortalComponent
  621. const LogseqQuickSearch = this.LogseqQuickSearch
  622. const blockContent = React.useMemo(() => {
  623. if (pageId && this.props.blockType === 'B') {
  624. return handlers?.queryBlockByUUID(pageId)?.content
  625. }
  626. }, [handlers?.queryBlockByUUID, pageId])
  627. const targetNotFound = this.props.blockType === 'B' && typeof blockContent !== 'string'
  628. const showingPortal = (!this.props.collapsed || isEditing) && !targetNotFound
  629. if (!renderers?.Page) {
  630. return null // not being correctly configured
  631. }
  632. const { Breadcrumb, PageNameLink } = renderers
  633. return (
  634. <HTMLContainer
  635. style={{
  636. pointerEvents: 'all',
  637. opacity: isErasing ? 0.2 : opacity,
  638. }}
  639. {...events}
  640. >
  641. <div
  642. onWheelCapture={stop}
  643. onPointerDown={stop}
  644. onPointerUp={stop}
  645. style={{
  646. width: '100%',
  647. height: '100%',
  648. pointerEvents: isEditing ? 'all' : 'none',
  649. }}
  650. >
  651. {isCreating ? (
  652. <LogseqQuickSearch onChange={onPageNameChanged} />
  653. ) : (
  654. <div
  655. className="tl-logseq-portal-container"
  656. style={{
  657. background: this.props.compact ? 'transparent' : fill,
  658. boxShadow: isBinding
  659. ? '0px 0px 0 var(--tl-binding-distance) var(--tl-binding)'
  660. : 'none',
  661. color: stroke,
  662. // @ts-expect-error ???
  663. '--ls-primary-background-color': !fill?.startsWith('var') ? fill : undefined,
  664. '--ls-primary-text-color': !stroke?.startsWith('var') ? stroke : undefined,
  665. '--ls-title-text-color': !stroke?.startsWith('var') ? stroke : undefined,
  666. }}
  667. >
  668. {!this.props.compact && !targetNotFound && (
  669. <LogseqPortalShapeHeader type={this.props.blockType ?? 'P'}>
  670. {this.props.blockType === 'P' ? (
  671. <PageNameLink pageName={pageId} />
  672. ) : (
  673. <Breadcrumb blockId={pageId} />
  674. )}
  675. </LogseqPortalShapeHeader>
  676. )}
  677. {targetNotFound && <div className="tl-target-not-found">Target not found</div>}
  678. {showingPortal && <PortalComponent {...componentProps} />}
  679. </div>
  680. )}
  681. </div>
  682. </HTMLContainer>
  683. )
  684. })
  685. ReactIndicator = observer(() => {
  686. const bounds = this.getBounds()
  687. return <rect width={bounds.width} height={bounds.height} fill="transparent" />
  688. })
  689. validateProps = (props: Partial<LogseqPortalShapeProps>) => {
  690. if (props.size !== undefined) {
  691. props.size[0] = Math.max(props.size[0], 240)
  692. props.size[1] = Math.max(props.size[1], HEADER_HEIGHT)
  693. }
  694. return withClampedStyles(props)
  695. }
  696. getShapeSVGJsx({ preview }: any) {
  697. // Do not need to consider the original point here
  698. const bounds = this.getBounds()
  699. return (
  700. <>
  701. <rect
  702. stroke={this.props.stroke}
  703. strokeWidth={this.props.strokeWidth ?? 2}
  704. fill="#aaa"
  705. width={bounds.width}
  706. height={HEADER_HEIGHT}
  707. />
  708. <rect
  709. y={HEADER_HEIGHT}
  710. fill={this.props.fill}
  711. stroke={this.props.stroke}
  712. strokeWidth={this.props.strokeWidth ?? 2}
  713. fillOpacity={this.props.opacity ?? 0.2}
  714. width={bounds.width}
  715. height={bounds.height - HEADER_HEIGHT}
  716. />
  717. <text
  718. style={{
  719. transformOrigin: 'top left',
  720. }}
  721. transform={`translate(${bounds.width / 2}, ${10 + bounds.height / 2})`}
  722. textAnchor="middle"
  723. fontFamily="var(--ls-font-family)"
  724. fontSize="32"
  725. fill={this.props.stroke}
  726. stroke={this.props.stroke}
  727. >
  728. {this.props.blockType === 'P' ? this.props.pageId : ''}
  729. </text>
  730. </>
  731. )
  732. }
  733. }