canvu-react 0.4.31 → 0.4.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tldraw.cjs CHANGED
@@ -52,10 +52,16 @@ function createCustomShapeItem(id, bounds, content) {
52
52
 
53
53
  // src/scene/link-item.ts
54
54
  var LINK_PLUGIN_KEY = "canvuLink";
55
- var LINK_CARD_BORDER = "#e2e8f0";
56
- var LINK_CARD_ACCENT = "#2563eb";
57
- var LINK_CARD_TITLE_COLOR = "#0f172a";
58
- var LINK_CARD_TEXT_COLOR = "#475569";
55
+ var DEFAULT_LINK_CARD_WIDTH = 320;
56
+ var DEFAULT_LINK_CARD_HEIGHT = 70;
57
+ var LINK_CARD_MIN_SCALE = 0.6;
58
+ var LINK_CARD_MAX_SCALE = 2.5;
59
+ var LINK_CARD_BORDER = "oklch(0.918 0.008 255)";
60
+ var LINK_CARD_BORDER_STRONG = "oklch(0.86 0.012 255)";
61
+ var LINK_CARD_ACCENT = "oklch(0.55 0.19 264)";
62
+ var LINK_CARD_ACCENT_DEEP = "oklch(0.46 0.18 264)";
63
+ var LINK_CARD_TITLE_COLOR = "oklch(0.26 0.022 265)";
64
+ var LINK_CARD_TEXT_COLOR = "oklch(0.55 0.022 265)";
59
65
  var clamp = (value, min, max) => Math.min(max, Math.max(min, value));
60
66
  var formatNumber = (value) => {
61
67
  const rounded = Math.round(value * 100) / 100;
@@ -71,49 +77,131 @@ var getLinkHostname = (href) => {
71
77
  }
72
78
  };
73
79
  var buildLinkTextBand = (band) => {
74
- const lineHeight = band.fontSize * 1.3;
80
+ const lineHeight = band.height;
75
81
  const weight = band.fontWeight != null ? `font-weight:${band.fontWeight};` : "";
76
- const lineClampStyle = band.clampLines ? `display:-webkit-box;-webkit-line-clamp:${band.clampLines};-webkit-box-orient:vertical;` : "";
77
- return `<foreignObject x="${formatNumber(band.x)}" y="${formatNumber(band.y)}" width="${formatNumber(Math.max(1, band.width))}" height="${formatNumber(Math.max(1, band.height))}"><div xmlns="http://www.w3.org/1999/xhtml" style="box-sizing:border-box;width:100%;height:100%;margin:0;font-family:system-ui,sans-serif;font-size:${formatNumber(band.fontSize)}px;line-height:${formatNumber(lineHeight)}px;color:${band.color};overflow:hidden;word-break:break-word;${lineClampStyle}${weight}">${escapeHtmlText(band.text)}</div></foreignObject>`;
82
+ return `<foreignObject x="${formatNumber(band.x)}" y="${formatNumber(band.y)}" width="${formatNumber(Math.max(1, band.width))}" height="${formatNumber(Math.max(1, band.height))}"><div xmlns="http://www.w3.org/1999/xhtml" style="box-sizing:border-box;width:100%;height:100%;margin:0;font-family:system-ui,sans-serif;font-size:${formatNumber(band.fontSize)}px;line-height:${formatNumber(lineHeight)}px;letter-spacing:-0.01em;color:${band.color};overflow:hidden;white-space:nowrap;text-overflow:ellipsis;${weight}">${escapeHtmlText(band.text)}</div></foreignObject>`;
78
83
  };
79
- function buildLinkCardSvg(width, height, link) {
84
+ var getLinkProtocol = (href) => {
85
+ try {
86
+ return new URL(href).protocol;
87
+ } catch {
88
+ return "";
89
+ }
90
+ };
91
+ var getLinkInitial = (hostname) => {
92
+ const first = hostname.trim().charAt(0).toUpperCase();
93
+ return first || "L";
94
+ };
95
+ var buildGoogleFaviconUrl = (hostname) => hostname ? `https://www.google.com/s2/favicons?domain=${encodeURIComponent(hostname)}&sz=64` : null;
96
+ var getStableLinkIdSuffix = (value) => {
97
+ let hash = 0;
98
+ for (const char of value) {
99
+ hash = hash * 31 + char.charCodeAt(0) >>> 0;
100
+ }
101
+ return hash.toString(36);
102
+ };
103
+ function buildLinkCardSvg(width, _height, link) {
80
104
  const cardWidth = Math.max(1, width);
81
- const cardHeight = Math.max(1, height);
105
+ const scale = cardWidth / DEFAULT_LINK_CARD_WIDTH;
106
+ const contentWidth = DEFAULT_LINK_CARD_WIDTH;
107
+ const contentHeight = DEFAULT_LINK_CARD_HEIGHT;
82
108
  const padding = 14;
83
- const badgeSize = clamp(Math.min(72, cardHeight - padding * 2), 28, 96);
84
- const textX = padding + badgeSize + 14;
85
- const textWidth = Math.max(1, cardWidth - textX - padding);
109
+ const badgeSize = 42;
110
+ const gap = 13;
111
+ const buttonSize = 34;
112
+ const textX = padding + badgeSize + gap;
113
+ const textWidth = Math.max(1, contentWidth - textX - buttonSize - gap - padding);
86
114
  const hostname = getLinkHostname(link.href);
87
115
  const title = link.title?.trim() || hostname || "Link";
88
- const description = link.description?.trim() || link.href;
89
- const titleY = padding;
90
- const titleHeight = clamp(cardHeight * 0.22, 18, 28);
91
- const hostY = titleY + titleHeight + 2;
92
- const hostHeight = 16;
93
- const descY = hostY + hostHeight + 4;
94
- const descHeight = Math.max(1, cardHeight - descY - padding);
95
- const badge = link.favicon ? `<clipPath id="canvu-link-badge"><rect x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" rx="12" /></clipPath>
96
- <rect x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" rx="12" fill="#f8fafc" stroke="${LINK_CARD_BORDER}" stroke-width="1" />
97
- <image href="${escapeXmlAttribute(link.favicon)}" x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" preserveAspectRatio="xMidYMid slice" clip-path="url(#canvu-link-badge)" />` : `<rect x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" rx="12" fill="${LINK_CARD_ACCENT}" fill-opacity="0.1" stroke="${LINK_CARD_ACCENT}" stroke-opacity="0.3" stroke-width="1" />
98
- <g transform="translate(${formatNumber(padding + badgeSize / 2)},${formatNumber(padding + badgeSize / 2)})" stroke="${LINK_CARD_ACCENT}" stroke-width="2.4" stroke-linecap="round" fill="none">
99
- <path d="M-9 3 a6 6 0 0 1 0 -8 l4 -4 a6 6 0 0 1 8 8 l-2 2" />
100
- <path d="M9 -3 a6 6 0 0 1 0 8 l-4 4 a6 6 0 0 1 -8 -8 l2 -2" />
101
- </g>`;
102
- const externalIconX = cardWidth - padding - 16;
103
- const externalIcon = `<g transform="translate(${formatNumber(externalIconX)},${formatNumber(padding)})" stroke="${LINK_CARD_TEXT_COLOR}" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" fill="none">
104
- <path d="M6 1 H11 V6" />
105
- <path d="M11 1 L4.5 7.5" />
106
- <path d="M9 7 V10 a1 1 0 0 1 -1 1 H2 a1 1 0 0 1 -1 -1 V4 a1 1 0 0 1 1 -1 H5" />
107
- </g>`;
116
+ const protocol = getLinkProtocol(link.href);
117
+ const subtitle = hostname || link.href;
118
+ const favicon = link.favicon?.trim() || buildGoogleFaviconUrl(hostname);
119
+ const idSuffix = getStableLinkIdSuffix(`${hostname}:${link.href}`);
120
+ const clipId = `canvu-link-favicon-${idSuffix}`;
121
+ const gradientId = `canvu-link-favicon-gradient-${idSuffix}`;
122
+ const buttonX = contentWidth - padding - buttonSize;
123
+ const buttonY = (contentHeight - buttonSize) / 2;
124
+ const isSecure = protocol === "https:";
125
+ const subtitleX = isSecure ? textX + 13 : textX;
126
+ const subtitleWidth = isSecure ? textWidth - 13 : textWidth;
127
+ const faviconImage = favicon ? `<image class="canvu-link-favicon-img" href="${escapeXmlAttribute(favicon)}" x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" preserveAspectRatio="xMidYMid slice" clip-path="url(#${clipId})" />` : "";
108
128
  return `
109
- <rect width="${formatNumber(cardWidth)}" height="${formatNumber(cardHeight)}" rx="16" fill="#ffffff" stroke="${LINK_CARD_BORDER}" stroke-width="1.5" />
110
- ${badge}
111
- ${externalIcon}
112
- ${buildLinkTextBand({ x: textX, y: titleY, width: textWidth - 18, height: titleHeight, text: title, fontSize: 15, color: LINK_CARD_TITLE_COLOR, fontWeight: 700, clampLines: 1 })}
113
- ${buildLinkTextBand({ x: textX, y: hostY, width: textWidth, height: hostHeight, text: hostname, fontSize: 12, color: LINK_CARD_ACCENT, clampLines: 1 })}
114
- ${buildLinkTextBand({ x: textX, y: descY, width: textWidth, height: descHeight, text: description, fontSize: 12, color: LINK_CARD_TEXT_COLOR, clampLines: 2 })}
129
+ <style>
130
+ .canvu-link-card-root .canvu-link-card { transition: transform .18s ease, filter .18s ease, stroke .18s ease; }
131
+ .canvu-link-card-root .canvu-link-open { opacity: 0; transform: translateX(-4px); transition: opacity .18s ease, transform .18s ease; }
132
+ .canvu-link-card-root:hover .canvu-link-card { transform: translateY(-2px); filter: drop-shadow(0 4px 14px oklch(0.4 0.05 265 / .08)) drop-shadow(0 1px 3px oklch(0.4 0.05 265 / .06)); stroke: ${LINK_CARD_BORDER_STRONG}; }
133
+ .canvu-link-card-root:hover .canvu-link-open { opacity: 1; transform: translateX(0); }
134
+ </style>
135
+ <g class="canvu-link-card-root" transform="scale(${formatNumber(scale)})">
136
+ <rect class="canvu-link-card" width="${formatNumber(contentWidth)}" height="${formatNumber(contentHeight)}" rx="16" fill="#ffffff" stroke="${LINK_CARD_BORDER}" stroke-width="1" filter="drop-shadow(0 1px 2px oklch(0.4 0.03 265 / .05)) drop-shadow(0 1px 1px oklch(0.4 0.03 265 / .04))" />
137
+ <defs>
138
+ <linearGradient id="${gradientId}" x1="8" y1="48" x2="48" y2="8" gradientUnits="userSpaceOnUse">
139
+ <stop stop-color="${LINK_CARD_ACCENT}" />
140
+ <stop offset="1" stop-color="${LINK_CARD_ACCENT_DEEP}" />
141
+ </linearGradient>
142
+ <clipPath id="${clipId}">
143
+ <rect x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" rx="11" />
144
+ </clipPath>
145
+ </defs>
146
+ <rect x="${formatNumber(padding)}" y="${formatNumber(padding)}" width="${formatNumber(badgeSize)}" height="${formatNumber(badgeSize)}" rx="11" fill="url(#${gradientId})" />
147
+ <text x="${formatNumber(padding + badgeSize / 2)}" y="${formatNumber(padding + badgeSize / 2 + 5)}" text-anchor="middle" font-family="system-ui,sans-serif" font-size="17" font-weight="700" fill="#ffffff">${escapeHtmlText(getLinkInitial(hostname))}</text>
148
+ ${faviconImage}
149
+ ${buildLinkTextBand({ x: textX, y: 16, width: textWidth, height: 19, text: title, fontSize: 14.5, color: LINK_CARD_TITLE_COLOR, fontWeight: 700 })}
150
+ ${isSecure ? `<g transform="translate(${formatNumber(textX)},40)" stroke="${LINK_CARD_TEXT_COLOR}" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" fill="none"><rect x="1.5" y="4.5" width="7" height="6" rx="1" /><path d="M3 4.5 V3 a2 2 0 0 1 4 0 v1.5" /></g>` : ""}
151
+ ${buildLinkTextBand({ x: subtitleX, y: 36, width: subtitleWidth, height: 17, text: subtitle, fontSize: 12.5, color: LINK_CARD_TEXT_COLOR })}
152
+ <g class="canvu-link-open" transform="translate(${formatNumber(buttonX)},${formatNumber(buttonY)})">
153
+ <rect width="${formatNumber(buttonSize)}" height="${formatNumber(buttonSize)}" rx="10" fill="#ffffff" stroke="${LINK_CARD_BORDER}" stroke-width="1" />
154
+ <g transform="translate(10,10)" stroke="${LINK_CARD_TEXT_COLOR}" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" fill="none">
155
+ <path d="M8 2 H14 V8" />
156
+ <path d="M14 2 L7 9" />
157
+ <path d="M12 10 V13 a1 1 0 0 1 -1 1 H3 a1 1 0 0 1 -1 -1 V5 a1 1 0 0 1 1 -1 H6" />
158
+ </g>
159
+ </g>
160
+ </g>
115
161
  `;
116
162
  }
163
+ var isCanvuLinkData = (value) => {
164
+ if (!value || typeof value !== "object") return false;
165
+ const candidate = value;
166
+ return typeof candidate.href === "string" && candidate.href.length > 0;
167
+ };
168
+ function getLinkData(item) {
169
+ const entry = item.pluginData?.[LINK_PLUGIN_KEY];
170
+ return isCanvuLinkData(entry) ? entry : null;
171
+ }
172
+ function rebuildLinkItemSvg(item) {
173
+ const link = getLinkData(item);
174
+ if (!link) return item;
175
+ const scale = clamp(
176
+ item.bounds.width / DEFAULT_LINK_CARD_WIDTH,
177
+ LINK_CARD_MIN_SCALE,
178
+ LINK_CARD_MAX_SCALE
179
+ );
180
+ const width = DEFAULT_LINK_CARD_WIDTH * scale;
181
+ const height = DEFAULT_LINK_CARD_HEIGHT * scale;
182
+ const bounds = {
183
+ ...item.bounds,
184
+ width,
185
+ height
186
+ };
187
+ const customInnerSvg = buildLinkCardSvg(
188
+ DEFAULT_LINK_CARD_WIDTH,
189
+ DEFAULT_LINK_CARD_HEIGHT,
190
+ link
191
+ );
192
+ return {
193
+ ...item,
194
+ x: bounds.x,
195
+ y: bounds.y,
196
+ bounds,
197
+ customIntrinsicSize: {
198
+ width: DEFAULT_LINK_CARD_WIDTH,
199
+ height: DEFAULT_LINK_CARD_HEIGHT
200
+ },
201
+ customInnerSvg,
202
+ childrenSvg: buildLinkCardSvg(width, height, link)
203
+ };
204
+ }
117
205
 
118
206
  // src/scene/text-svg.ts
119
207
  function escapeSvgTextContent(s) {
@@ -817,6 +905,9 @@ function rebuildItemSvg(item) {
817
905
  )
818
906
  };
819
907
  }
908
+ if (getLinkData(item)) {
909
+ return rebuildLinkItemSvg(item);
910
+ }
820
911
  if (k === "custom" && item.customIntrinsicSize && item.customInnerSvg) {
821
912
  const b = normalizeRect(item.bounds);
822
913
  return {