Skip to content

Commit

Permalink
use util/copy-to-clipboard!
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc authored and tiensonqin committed Mar 8, 2023
1 parent 316fa2f commit 0a89ac5
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/frontend/extensions/tldraw.cljs
Expand Up @@ -93,6 +93,7 @@
:isMobile util/mobile?
:saveAsset save-asset-handler
:makeAssetUrl editor-handler/make-asset-url
:copyToClipboard (fn [text, html] (util/copy-to-clipboard! text html))
:getRedirectPageName (fn [page-name-or-uuid] (model/get-redirect-page-name page-name-or-uuid))
:insertFirstPageBlock (fn [page-name] (editor-handler/insert-first-page-block-if-not-exists! page-name {:redirect? false}))
:addNewWhiteboard (fn [page-name]
Expand Down
5 changes: 4 additions & 1 deletion tldraw/apps/tldraw-logseq/src/app.tsx
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TLDocumentModel } from '@tldraw/core'
import type { TLDocumentModel } from '@tldraw/core'
import {
AppCanvas,
AppProvider,
Expand All @@ -15,6 +15,7 @@ import { ContextMenu } from './components/ContextMenu'
import { QuickLinks } from './components/QuickLinks'
import { useDrop } from './hooks/useDrop'
import { usePaste } from './hooks/usePaste'
import { useCopy } from './hooks/useCopy'
import { useQuickAdd } from './hooks/useQuickAdd'
import {
BoxTool,
Expand Down Expand Up @@ -95,6 +96,7 @@ const AppInner = ({
}: Omit<LogseqTldrawProps, 'renderers' | 'handlers'>) => {
const onDrop = useDrop()
const onPaste = usePaste()
const onCopy = useCopy()
const onQuickAdd = useQuickAdd()

const onPersistOnDiff: TLReactCallbacks<Shape>['onPersist'] = React.useCallback(
Expand All @@ -110,6 +112,7 @@ const AppInner = ({
Tools={tools}
onDrop={onDrop}
onPaste={onPaste}
onCopy={onCopy}
onCanvasDBClick={onQuickAdd}
onPersist={onPersistOnDiff}
model={model}
Expand Down
11 changes: 11 additions & 0 deletions tldraw/apps/tldraw-logseq/src/hooks/useCopy.ts
@@ -0,0 +1,11 @@
import type { TLReactCallbacks } from '@tldraw/react'
import * as React from 'react'
import { LogseqContext } from '../lib/logseq-context'

export function useCopy() {
const { handlers } = React.useContext(LogseqContext)

return React.useCallback<TLReactCallbacks['onCopy']>((app, {text, html}) => {
handlers.copyToClipboard(text, html)
}, [])
}
1 change: 1 addition & 0 deletions tldraw/apps/tldraw-logseq/src/lib/logseq-context.ts
Expand Up @@ -55,6 +55,7 @@ export interface LogseqContextValue {
makeAssetUrl: (relativeUrl: string) => string
sidebarAddBlock: (uuid: string, type: 'block' | 'page') => void
redirectToPage: (uuidOrPageName: string) => void
copyToClipboard: (text:string, html: string) => void
}
}

Expand Down
13 changes: 4 additions & 9 deletions tldraw/packages/core/src/lib/TLApp/TLApp.ts
Expand Up @@ -560,15 +560,10 @@ export class TLApp<

const shapeBlockRefs = this.selectedShapesArray.map(s => `((${s.props.id}))`).join(' ')

// FIXME: use `writeClipboard` in frontend.utils
navigator.clipboard.write([
new ClipboardItem({
'text/html': new Blob([tldrawString], { type: 'text/html' }),
'text/plain': new Blob([shapeBlockRefs], {
type: 'text/plain',
}),
}),
])
this.notify('copy', {
text: shapeBlockRefs,
html: tldrawString
})
}
}

Expand Down
9 changes: 9 additions & 0 deletions tldraw/packages/core/src/types/types.ts
Expand Up @@ -141,6 +141,11 @@ export type TLPasteEventInfo = {
fromDrop?: boolean
}

export type TLCopyEventInfo = {
text: string
html: string
}

/* --------------------- Events --------------------- */

export type TLSubscriptionEvent =
Expand Down Expand Up @@ -188,6 +193,10 @@ export type TLSubscriptionEvent =
event: 'drop'
info: { dataTransfer: DataTransfer; point: number[] }
}
| {
event: 'copy'
info: TLCopyEventInfo
}
| {
event: 'paste'
info: TLPasteEventInfo
Expand Down
2 changes: 2 additions & 0 deletions tldraw/packages/react/src/hooks/useSetup.ts
Expand Up @@ -20,6 +20,7 @@ export function useSetup<
onDeleteShapes,
onDrop,
onPaste,
onCopy,
onCanvasDBClick,
} = props

Expand Down Expand Up @@ -51,6 +52,7 @@ export function useSetup<
if (onDeleteAssets) unsubs.push(app.subscribe('delete-assets', onDeleteAssets))
if (onDrop) unsubs.push(app.subscribe('drop', onDrop))
if (onPaste) unsubs.push(app.subscribe('paste', onPaste))
if (onCopy) unsubs.push(app.subscribe('copy', onCopy))
if (onCanvasDBClick) unsubs.push(app.subscribe('canvas-dbclick', onCanvasDBClick))
// Kind of unusual, is this the right pattern?
return () => unsubs.forEach(unsub => unsub())
Expand Down
1 change: 1 addition & 0 deletions tldraw/packages/react/src/types/TLReactSubscriptions.tsx
Expand Up @@ -41,4 +41,5 @@ export interface TLReactCallbacks<
onDrop: TLReactCallback<S, R, 'drop'>
onCanvasDBClick: TLReactCallback<S, R, 'canvas-dbclick'>
onPaste: TLReactCallback<S, R, 'paste'>
onCopy: TLReactCallback<S, R, 'copy'>
}

0 comments on commit 0a89ac5

Please sign in to comment.