Skip to content

Commit

Permalink
Fix (Whiteboards): paste type order (logseq#8416)
Browse files Browse the repository at this point in the history
* fix: set min width to tweet shape

* fix logseq#8407

Co-authored-by: Bad3r <[email protected]>
  • Loading branch information
sprocketc and Bad3r authored Jan 24, 2023
1 parent e2ff4b9 commit 5487ca6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
54 changes: 32 additions & 22 deletions tldraw/apps/tldraw-logseq/src/hooks/usePaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ const handleCreatingShapes = async (
tryCreateShapeFromFiles,
tryCreateShapeFromPageName,
tryCreateShapeFromBlockUUID,
tryCreateShapeFromTextPlain,
tryCreateShapeFromTextHTML,
tryCreateShapeFromTextPlain
tryCreateLogseqPortalShapesFromString
)(dataTransfer)
}

async function tryCreateShapesFromClipboard() {
const items = await navigator.clipboard.read()
const createShapesFn = tryCreateShapeHelper(
tryCreateShapeFromTextPlain,
tryCreateShapeFromTextHTML,
tryCreateShapeFromTextPlain
tryCreateLogseqPortalShapesFromString
)
const allShapes = (await Promise.all(items.map(item => createShapesFn(item))))
.flat()
Expand Down Expand Up @@ -220,7 +222,7 @@ const handleCreatingShapes = async (
: [text]
// ensure all uuid in blockUUIDs is persisted
window.logseq?.api?.set_blocks_id?.(blockUUIDs)
const tasks = blockUUIDs.map(uuid => tryCreateLogseqPortalShapesFromString(`((${uuid}))`))
const tasks = blockUUIDs.map(uuid => tryCreateLogseqPortalShapesFromUUID(`((${uuid}))`))
const newShapes = (await Promise.all(tasks)).flat().filter(isNonNullable)
return newShapes.map((s, idx) => {
// if there are multiple shapes, shift them to the right
Expand All @@ -240,7 +242,7 @@ const handleCreatingShapes = async (
if (rawText) {
const text = rawText.trim()

return tryCreateLogseqPortalShapesFromString(`[[${text}]]`)
return tryCreateLogseqPortalShapesFromUUID(`[[${text}]]`)
}
return null
}
Expand All @@ -252,7 +254,7 @@ const handleCreatingShapes = async (
return tryCreateShapeHelper(
tryCreateShapeFromURL,
tryCreateShapeFromIframeString,
tryCreateLogseqPortalShapesFromString
tryCreateLogseqPortalShapesFromUUID
)(text)
}

Expand Down Expand Up @@ -317,7 +319,7 @@ const handleCreatingShapes = async (
return null
}

async function tryCreateLogseqPortalShapesFromString(rawText: string) {
async function tryCreateLogseqPortalShapesFromUUID(rawText: string) {
if (/^\(\(.*\)\)$/.test(rawText) && rawText.length === NIL_UUID.length + 4) {
const blockRef = rawText.slice(2, -2)
if (validUUID(blockRef)) {
Expand Down Expand Up @@ -350,22 +352,30 @@ const handleCreatingShapes = async (
]
}

// Otherwise, creating a new block that belongs to the current whiteboard
const uuid = handlers?.addNewBlock(rawText)
if (uuid) {
// create text shape
return [
{
...LogseqPortalShape.defaultProps,
size: [400, 0], // use 0 here to enable auto-resize
point: [point[0], point[1]],
pageId: uuid,
fill: app.settings.color,
stroke: app.settings.color,
blockType: 'B' as 'B',
compact: true,
},
]
return null
}

async function tryCreateLogseqPortalShapesFromString(item: DataTransfer | ClipboardItem) {
const rawText = await getDataFromType(item, 'text/plain')
if (rawText) {
const text = rawText.trim()
// Create a new block that belongs to the current whiteboard
const uuid = handlers?.addNewBlock(text)
if (uuid) {
// create text shape
return [
{
...LogseqPortalShape.defaultProps,
size: [400, 0], // use 0 here to enable auto-resize
point: [point[0], point[1]],
pageId: uuid,
fill: app.settings.color,
stroke: app.settings.color,
blockType: 'B' as 'B',
compact: true,
},
]
}
}

return null
Expand Down
2 changes: 1 addition & 1 deletion tldraw/apps/tldraw-logseq/src/lib/shapes/TweetShape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class TweetShape extends TLBoxShape<TweetShapeProps> {

validateProps = (props: Partial<TweetShapeProps>) => {
if (props.size !== undefined) {
props.size[0] = Math.min(Math.max(props.size[0], 1), 550)
props.size[0] = Math.min(Math.max(props.size[0], 300), 550)
props.size[1] = Math.max(props.size[1], 1)
}
return withClampedStyles(this, props)
Expand Down

0 comments on commit 5487ca6

Please sign in to comment.