|
|
@@ -1,6 +1,5 @@
|
|
|
import {
|
|
|
BoundsUtils,
|
|
|
- fileToBase64,
|
|
|
getSizeFromSrc,
|
|
|
TLAsset,
|
|
|
TLBinding,
|
|
|
@@ -23,11 +22,6 @@ const isValidURL = (url: string) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-const getYoutubeId = (url: string) => {
|
|
|
- const match = url.match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#&?]*).*/)
|
|
|
- return match && match[2].length === 11 ? match[2] : null
|
|
|
-}
|
|
|
-
|
|
|
export function usePaste(context: LogseqContextValue) {
|
|
|
const { handlers } = context
|
|
|
|
|
|
@@ -42,26 +36,30 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
const shapesToCreate: Shape['props'][] = []
|
|
|
const bindingsToCreate: TLBinding[] = []
|
|
|
|
|
|
+ async function createAsset(file: File): Promise<string | null> {
|
|
|
+ return await handlers.saveAsset(file)
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: handle PDF?
|
|
|
async function handleFiles(files: File[]) {
|
|
|
const IMAGE_EXTENSIONS = ['.png', '.svg', '.jpg', '.jpeg', '.gif']
|
|
|
|
|
|
- const blobs = files.filter(file => {
|
|
|
+ for (const file of files) {
|
|
|
// Get extension, verify that it's an image
|
|
|
const extensionMatch = file.name.match(/\.[0-9a-z]+$/i)
|
|
|
- if (!extensionMatch) throw Error('No extension.')
|
|
|
+ if (!extensionMatch) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
const extension = extensionMatch[0].toLowerCase()
|
|
|
- return IMAGE_EXTENSIONS.includes(extension)
|
|
|
- })
|
|
|
-
|
|
|
- return handleBlobs(blobs)
|
|
|
- }
|
|
|
-
|
|
|
- async function handleBlobs(blobs: Blob[]) {
|
|
|
- for (const blob of blobs) {
|
|
|
+ if (!IMAGE_EXTENSIONS.includes(extension)) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
try {
|
|
|
// Turn the image into a base64 dataurl
|
|
|
- const dataurl = await fileToBase64(blob)
|
|
|
- if (typeof dataurl !== 'string') continue
|
|
|
+ const dataurl = await createAsset(file)
|
|
|
+ if (!dataurl) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
// Do we already have an asset for this image?
|
|
|
const existingAsset = Object.values(app.assets).find(asset => asset.src === dataurl)
|
|
|
if (existingAsset) {
|
|
|
@@ -73,7 +71,7 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
id: assetId,
|
|
|
type: 'image',
|
|
|
src: dataurl,
|
|
|
- size: await getSizeFromSrc(dataurl),
|
|
|
+ size: await getSizeFromSrc(handlers.makeAssetUrl(dataurl)),
|
|
|
}
|
|
|
assetsToCreate.push(asset)
|
|
|
} catch (error) {
|
|
|
@@ -82,16 +80,6 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async function handleImage(item: ClipboardItem) {
|
|
|
- const firstImageType = item.types.find(type => type.startsWith('image'))
|
|
|
- if (firstImageType) {
|
|
|
- const blob = await item.getType(firstImageType)
|
|
|
- await handleBlobs([blob])
|
|
|
- return true
|
|
|
- }
|
|
|
- return false
|
|
|
- }
|
|
|
-
|
|
|
async function handleHTML(item: ClipboardItem) {
|
|
|
if (item.types.includes('text/html')) {
|
|
|
const blob = await item.getType('text/html')
|
|
|
@@ -100,68 +88,75 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
shapesToCreate.push({
|
|
|
...HTMLShape.defaultProps,
|
|
|
html: rawText,
|
|
|
- parentId: app.currentPageId,
|
|
|
point: [point[0], point[1]],
|
|
|
})
|
|
|
return true
|
|
|
}
|
|
|
+ return false
|
|
|
+ }
|
|
|
|
|
|
+ async function handleTextPlain(item: ClipboardItem) {
|
|
|
if (item.types.includes('text/plain')) {
|
|
|
const blob = await item.getType('text/plain')
|
|
|
const rawText = (await blob.text()).trim()
|
|
|
- // if rawText is iframe text
|
|
|
- if (rawText.startsWith('<iframe')) {
|
|
|
- shapesToCreate.push({
|
|
|
- ...HTMLShape.defaultProps,
|
|
|
- html: rawText,
|
|
|
- parentId: app.currentPageId,
|
|
|
- point: [point[0], point[1]],
|
|
|
- })
|
|
|
+
|
|
|
+ if (handleURL(rawText)) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (handleIframe(rawText)) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (handleTldrawShapes(rawText)) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if (await handleLogseqPortalShapes(rawText)) {
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
- async function handleLogseqShapes(item: ClipboardItem) {
|
|
|
- if (item.types.includes('text/plain')) {
|
|
|
- const blob = await item.getType('text/plain')
|
|
|
- const rawText = (await blob.text()).trim()
|
|
|
-
|
|
|
- try {
|
|
|
- const data = JSON.parse(rawText)
|
|
|
- if (data.type === 'logseq/whiteboard-shapes') {
|
|
|
- const shapes = data.shapes as TLShapeModel[]
|
|
|
- const commonBounds = BoundsUtils.getCommonBounds(
|
|
|
- shapes.map(shape => ({
|
|
|
- minX: shape.point?.[0] ?? point[0],
|
|
|
- minY: shape.point?.[1] ?? point[1],
|
|
|
- width: shape.size?.[0] ?? 4,
|
|
|
- height: shape.size?.[1] ?? 4,
|
|
|
- maxX: (shape.point?.[0] ?? point[0]) + (shape.size?.[0] ?? 4),
|
|
|
- maxY: (shape.point?.[1] ?? point[1]) + (shape.size?.[1] ?? 4),
|
|
|
- }))
|
|
|
- )
|
|
|
- const clonedShapes = shapes.map(shape => {
|
|
|
- return {
|
|
|
- ...shape,
|
|
|
- parentId: app.currentPageId,
|
|
|
- point: [
|
|
|
- point[0] + shape.point![0] - commonBounds.minX,
|
|
|
- point[1] + shape.point![1] - commonBounds.minY,
|
|
|
- ],
|
|
|
- }
|
|
|
- })
|
|
|
- // @ts-expect-error - This is a valid shape
|
|
|
- shapesToCreate.push(...clonedShapes)
|
|
|
+ function handleTldrawShapes(rawText: string) {
|
|
|
+ try {
|
|
|
+ const data = JSON.parse(rawText)
|
|
|
+ if (data.type === 'logseq/whiteboard-shapes') {
|
|
|
+ debugger
|
|
|
+ const shapes = data.shapes as TLShapeModel[]
|
|
|
+ const commonBounds = BoundsUtils.getCommonBounds(
|
|
|
+ shapes.map(shape => ({
|
|
|
+ minX: shape.point?.[0] ?? point[0],
|
|
|
+ minY: shape.point?.[1] ?? point[1],
|
|
|
+ width: shape.size?.[0] ?? 4,
|
|
|
+ height: shape.size?.[1] ?? 4,
|
|
|
+ maxX: (shape.point?.[0] ?? point[0]) + (shape.size?.[0] ?? 4),
|
|
|
+ maxY: (shape.point?.[1] ?? point[1]) + (shape.size?.[1] ?? 4),
|
|
|
+ }))
|
|
|
+ )
|
|
|
+ const clonedShapes = shapes.map(shape => {
|
|
|
+ return {
|
|
|
+ ...shape,
|
|
|
+ point: [
|
|
|
+ point[0] + shape.point![0] - commonBounds.minX,
|
|
|
+ point[1] + shape.point![1] - commonBounds.minY,
|
|
|
+ ],
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // @ts-expect-error - This is a valid shape
|
|
|
+ shapesToCreate.push(...clonedShapes)
|
|
|
|
|
|
- // Try to rebinding the shapes to the new assets
|
|
|
- shapesToCreate.forEach((s, idx) => {
|
|
|
- if (s.handles) {
|
|
|
- Object.values(s.handles).forEach(h => {
|
|
|
- if (h.bindingId) {
|
|
|
- // try to bind the new shape
|
|
|
- const binding = app.currentPage.bindings[h.bindingId]
|
|
|
+ // Try to rebinding the shapes to the new assets
|
|
|
+ shapesToCreate.forEach((s, idx) => {
|
|
|
+ if (s.handles) {
|
|
|
+ Object.values(s.handles).forEach(h => {
|
|
|
+ if (h.bindingId) {
|
|
|
+ // try to bind the new shape
|
|
|
+ const binding = app.currentPage.bindings[h.bindingId]
|
|
|
+ // FIXME: if copy from a different whiteboard, the binding info
|
|
|
+ // will not be available
|
|
|
+ if (binding) {
|
|
|
// if the copied binding from/to is in the source
|
|
|
const oldFromIdx = shapes.findIndex(s => s.id === binding.fromId)
|
|
|
const oldToIdx = shapes.findIndex(s => s.id === binding.toId)
|
|
|
@@ -178,93 +173,112 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
h.bindingId = undefined
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- return true
|
|
|
- }
|
|
|
- } catch {
|
|
|
- if (/^\(\(.*\)\)$/.test(rawText) && rawText.length === NIL_UUID.length + 4) {
|
|
|
- const blockRef = rawText.slice(2, -2)
|
|
|
- if (validUUID(blockRef)) {
|
|
|
- shapesToCreate.push({
|
|
|
- ...LogseqPortalShape.defaultProps,
|
|
|
- parentId: app.currentPageId,
|
|
|
- point: [point[0], point[1]],
|
|
|
- size: [400, 0], // use 0 here to enable auto-resize
|
|
|
- pageId: blockRef,
|
|
|
- blockType: 'B',
|
|
|
+ }
|
|
|
})
|
|
|
- return true
|
|
|
}
|
|
|
- } else if (/^\[\[.*\]\]$/.test(rawText)) {
|
|
|
- const pageName = rawText.slice(2, -2)
|
|
|
- shapesToCreate.push({
|
|
|
- ...LogseqPortalShape.defaultProps,
|
|
|
- parentId: app.currentPageId,
|
|
|
- point: [point[0], point[1]],
|
|
|
- size: [400, 0], // use 0 here to enable auto-resize
|
|
|
- pageId: pageName,
|
|
|
- blockType: 'P',
|
|
|
- })
|
|
|
- return true
|
|
|
- } else if (isValidURL(rawText)) {
|
|
|
- const youtubeId = getYoutubeId(rawText)
|
|
|
- if (youtubeId) {
|
|
|
- shapesToCreate.push({
|
|
|
- ...YouTubeShape.defaultProps,
|
|
|
- embedId: youtubeId,
|
|
|
- parentId: app.currentPageId,
|
|
|
- point: [point[0], point[1]],
|
|
|
- })
|
|
|
- return true
|
|
|
- }
|
|
|
- // ??? deal with normal URLs?
|
|
|
- }
|
|
|
- const uuid = handlers?.addNewBlock(rawText)
|
|
|
- if (uuid) {
|
|
|
- // create text shape
|
|
|
- shapesToCreate.push({
|
|
|
- ...LogseqPortalShape.defaultProps,
|
|
|
- id: uniqueId(),
|
|
|
- parentId: app.currentPageId,
|
|
|
- size: [400, 0], // use 0 here to enable auto-resize
|
|
|
- point: [point[0], point[1]],
|
|
|
- pageId: uuid,
|
|
|
- blockType: 'B',
|
|
|
- compact: true,
|
|
|
- })
|
|
|
- return true
|
|
|
- }
|
|
|
+ })
|
|
|
+ return true
|
|
|
}
|
|
|
+ } catch (err) {
|
|
|
+ console.error(err)
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
- if (files && files.length > 0) {
|
|
|
- await handleFiles(files)
|
|
|
- } else {
|
|
|
- for (const item of await navigator.clipboard.read()) {
|
|
|
- try {
|
|
|
- let handled = await handleImage(item)
|
|
|
+ function handleURL(rawText: string) {
|
|
|
+ if (isValidURL(rawText)) {
|
|
|
+ const getYoutubeId = (url: string) => {
|
|
|
+ const match = url.match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#&?]*).*/)
|
|
|
+ return match && match[2].length === 11 ? match[2] : null
|
|
|
+ }
|
|
|
+ const youtubeId = getYoutubeId(rawText)
|
|
|
+ if (youtubeId) {
|
|
|
+ shapesToCreate.push({
|
|
|
+ ...YouTubeShape.defaultProps,
|
|
|
+ embedId: youtubeId,
|
|
|
+ point: [point[0], point[1]],
|
|
|
+ })
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ // ??? deal with normal URLs?
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
|
|
|
- if (!handled && !shiftKey) {
|
|
|
- handled = await handleHTML(item)
|
|
|
- }
|
|
|
+ function handleIframe(rawText: string) {
|
|
|
+ // if rawText is iframe text
|
|
|
+ if (rawText.startsWith('<iframe')) {
|
|
|
+ shapesToCreate.push({
|
|
|
+ ...HTMLShape.defaultProps,
|
|
|
+ html: rawText,
|
|
|
+ point: [point[0], point[1]],
|
|
|
+ })
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ async function handleLogseqPortalShapes(rawText: string) {
|
|
|
+ if (/^\(\(.*\)\)$/.test(rawText) && rawText.length === NIL_UUID.length + 4) {
|
|
|
+ const blockRef = rawText.slice(2, -2)
|
|
|
+ if (validUUID(blockRef)) {
|
|
|
+ shapesToCreate.push({
|
|
|
+ ...LogseqPortalShape.defaultProps,
|
|
|
+ point: [point[0], point[1]],
|
|
|
+ size: [400, 0], // use 0 here to enable auto-resize
|
|
|
+ pageId: blockRef,
|
|
|
+ blockType: 'B',
|
|
|
+ })
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ } else if (/^\[\[.*\]\]$/.test(rawText)) {
|
|
|
+ const pageName = rawText.slice(2, -2)
|
|
|
+ shapesToCreate.push({
|
|
|
+ ...LogseqPortalShape.defaultProps,
|
|
|
+ point: [point[0], point[1]],
|
|
|
+ size: [400, 0], // use 0 here to enable auto-resize
|
|
|
+ pageId: pageName,
|
|
|
+ blockType: 'P',
|
|
|
+ })
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ const uuid = handlers?.addNewBlock(rawText)
|
|
|
+ if (uuid) {
|
|
|
+ // create text shape
|
|
|
+ shapesToCreate.push({
|
|
|
+ ...LogseqPortalShape.defaultProps,
|
|
|
+ id: uniqueId(),
|
|
|
+ size: [400, 0], // use 0 here to enable auto-resize
|
|
|
+ point: [point[0], point[1]],
|
|
|
+ pageId: uuid,
|
|
|
+ blockType: 'B',
|
|
|
+ compact: true,
|
|
|
+ })
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
|
|
|
+ try {
|
|
|
+ if (files && files.length > 0) {
|
|
|
+ await handleFiles(files)
|
|
|
+ } else {
|
|
|
+ for (const item of await navigator.clipboard.read()) {
|
|
|
+ let handled = !shiftKey ? await handleHTML(item) : false
|
|
|
if (!handled) {
|
|
|
- await handleLogseqShapes(item)
|
|
|
+ await handleTextPlain(item)
|
|
|
}
|
|
|
- } catch (error) {
|
|
|
- console.error(error)
|
|
|
}
|
|
|
}
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error)
|
|
|
}
|
|
|
|
|
|
const allShapesToAdd: TLShapeModel[] = [
|
|
|
+ // assets to images
|
|
|
...assetsToCreate.map((asset, i) => ({
|
|
|
type: 'image',
|
|
|
- parentId: app.currentPageId,
|
|
|
// TODO: Should be place near the last edited shape
|
|
|
point: [point[0] - asset.size[0] / 2 + i * 16, point[1] - asset.size[1] / 2 + i * 16],
|
|
|
size: asset.size,
|
|
|
@@ -275,6 +289,7 @@ export function usePaste(context: LogseqContextValue) {
|
|
|
].map(shape => {
|
|
|
return {
|
|
|
...shape,
|
|
|
+ parentId: app.currentPageId,
|
|
|
id: uniqueId(),
|
|
|
}
|
|
|
})
|