/
BookmarkShapeUtil.tsx
196 lines (174 loc) · 4.89 KB
/
BookmarkShapeUtil.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
AssetRecordType,
BaseBoxShapeUtil,
Editor,
HTMLContainer,
T,
TLAssetId,
TLBookmarkAsset,
TLBookmarkShape,
TLOnBeforeCreateHandler,
TLOnBeforeUpdateHandler,
bookmarkShapeMigrations,
bookmarkShapeProps,
debounce,
getHashForString,
stopEventPropagation,
toDomPrecision,
} from '@tldraw/editor'
import { truncateStringWithEllipsis } from '../../utils/text/text'
import { HyperlinkButton } from '../shared/HyperlinkButton'
import { getRotatedBoxShadow } from '../shared/rotated-box-shadow'
/** @public */
export class BookmarkShapeUtil extends BaseBoxShapeUtil<TLBookmarkShape> {
static override type = 'bookmark' as const
static override props = bookmarkShapeProps
static override migrations = bookmarkShapeMigrations
override canResize = () => false
override hideSelectionBoundsFg = () => true
override getDefaultProps(): TLBookmarkShape['props'] {
return {
url: '',
w: 300,
h: 320,
assetId: null,
}
}
override component(shape: TLBookmarkShape) {
const asset = (
shape.props.assetId ? this.editor.getAsset(shape.props.assetId) : null
) as TLBookmarkAsset
const pageRotation = this.editor.getShapePageTransform(shape)!.rotation()
const address = getHumanReadableAddress(shape)
return (
<HTMLContainer>
<div
className="tl-bookmark__container"
style={{
boxShadow: getRotatedBoxShadow(pageRotation),
}}
>
<div className="tl-bookmark__image_container">
{asset?.props.image ? (
<img
className="tl-bookmark__image"
draggable={false}
src={asset?.props.image}
alt={asset?.props.title || ''}
/>
) : (
<div className="tl-bookmark__placeholder" />
)}
<HyperlinkButton url={shape.props.url} zoomLevel={this.editor.getZoomLevel()} />
</div>
<div className="tl-bookmark__copy_container">
{asset?.props.title && (
<h2 className="tl-bookmark__heading">
{truncateStringWithEllipsis(asset?.props.title || '', 54)}
</h2>
)}
{asset?.props.description && (
<p className="tl-bookmark__description">
{truncateStringWithEllipsis(asset?.props.description || '', 128)}
</p>
)}
<a
className="tl-bookmark__link"
href={shape.props.url || ''}
target="_blank"
rel="noopener noreferrer"
onPointerDown={stopEventPropagation}
onPointerUp={stopEventPropagation}
onClick={stopEventPropagation}
>
{truncateStringWithEllipsis(address, 45)}
</a>
</div>
</div>
</HTMLContainer>
)
}
override indicator(shape: TLBookmarkShape) {
return (
<rect
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
rx="6"
ry="6"
/>
)
}
override onBeforeCreate?: TLOnBeforeCreateHandler<TLBookmarkShape> = (shape) => {
updateBookmarkAssetOnUrlChange(this.editor, shape)
}
override onBeforeUpdate?: TLOnBeforeUpdateHandler<TLBookmarkShape> = (prev, shape) => {
if (prev.props.url !== shape.props.url) {
if (!T.linkUrl.isValid(shape.props.url)) {
return { ...shape, props: { ...shape.props, url: prev.props.url } }
} else {
updateBookmarkAssetOnUrlChange(this.editor, shape)
}
}
}
}
/** @internal */
export const getHumanReadableAddress = (shape: TLBookmarkShape) => {
try {
const url = new URL(shape.props.url)
const path = url.pathname.replace(/\/*$/, '')
return `${url.hostname}${path}`
} catch (e) {
return shape.props.url
}
}
function updateBookmarkAssetOnUrlChange(editor: Editor, shape: TLBookmarkShape) {
const { url } = shape.props
// Derive the asset id from the URL
const assetId: TLAssetId = AssetRecordType.createId(getHashForString(url))
if (editor.getAsset(assetId)) {
// Existing asset for this URL?
if (shape.props.assetId !== assetId) {
editor.updateShapes<TLBookmarkShape>([
{
id: shape.id,
type: shape.type,
props: { assetId },
},
])
}
} else {
// No asset for this URL?
// First, clear out the existing asset reference
editor.updateShapes<TLBookmarkShape>([
{
id: shape.id,
type: shape.type,
props: { assetId: null },
},
])
// Then try to asyncronously create a new one
createBookmarkAssetOnUrlChange(editor, shape)
}
}
const createBookmarkAssetOnUrlChange = debounce(async (editor: Editor, shape: TLBookmarkShape) => {
const { url } = shape.props
// Create the asset using the external content manager's createAssetFromUrl method.
// This may be overwritten by the user (for example, we overwrite it on tldraw.com)
const asset = await editor.getAssetForExternalContent({ type: 'url', url })
if (!asset) {
// No asset? Just leave the bookmark as a null assetId.
return
}
editor.batch(() => {
// Create the new asset
editor.createAssets([asset])
// And update the shape
editor.updateShapes<TLBookmarkShape>([
{
id: shape.id,
type: shape.type,
props: { assetId: asset.id },
},
])
})
}, 500)