tldraw
Version:
A tiny little drawing editor.
220 lines (219 loc) • 8.13 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var RichTextLabel_exports = {};
__export(RichTextLabel_exports, {
RichTextLabel: () => RichTextLabel,
RichTextSVG: () => RichTextSVG
});
module.exports = __toCommonJS(RichTextLabel_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_editor = require("@tldraw/editor");
var import_classnames = __toESM(require("classnames"), 1);
var import_react = __toESM(require("react"), 1);
var import_richText = require("../../utils/text/richText");
var import_RichTextArea = require("../text/RichTextArea");
var import_default_shape_constants = require("./default-shape-constants");
var import_legacyProps = require("./legacyProps");
var import_useEditableRichText = require("./useEditableRichText");
const RichTextLabel = import_react.default.memo(function RichTextLabel2({
shapeId,
type,
richText,
labelColor,
font,
fontSize,
lineHeight,
align,
verticalAlign,
wrap,
isSelected,
padding = 0,
onKeyDown: handleKeyDownCustom,
classNamePrefix,
style,
textWidth,
textHeight,
hasCustomTabBehavior,
showTextOutline = true
}) {
const editor = (0, import_editor.useEditor)();
const isDragging = import_react.default.useRef(false);
const { rInput, isEmpty, isEditing, isReadyForEditing, ...editableTextRest } = (0, import_useEditableRichText.useEditableRichText)(shapeId, type, richText);
const html = (0, import_react.useMemo)(() => {
if (richText) {
return (0, import_richText.renderHtmlFromRichText)(editor, richText);
}
}, [editor, richText]);
const selectToolActive = (0, import_editor.useValue)(
"isSelectToolActive",
() => editor.getCurrentToolId() === "select",
[editor]
);
(0, import_editor.useReactor)(
"isDragging",
() => {
editor.getInstanceState();
isDragging.current = editor.inputs.getIsDragging();
},
[editor]
);
const legacyAlign = (0, import_legacyProps.isLegacyAlign)(align);
const handlePointerDown = (e) => {
if (e.target instanceof HTMLElement && (e.target.tagName === "A" || e.target.closest("a"))) {
(0, import_editor.preventDefault)(e);
if (!selectToolActive) return;
const link = e.target.closest("a")?.getAttribute("href") ?? "";
const handlePointerUp = (e2) => {
if (e2.name !== "pointer_up" || !link) return;
if (!isDragging.current) {
(0, import_editor.openWindow)(link, "_blank", false);
}
editor.off("event", handlePointerUp);
};
editor.on("event", handlePointerUp);
}
};
if (!isEditing && isEmpty) return null;
const cssPrefix = classNamePrefix || "tl-text";
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"div",
{
className: (0, import_classnames.default)(
`${cssPrefix}-label tl-text-wrapper tl-rich-text-wrapper`,
showTextOutline ? "tl-text__outline" : "tl-text__no-outline"
),
"aria-hidden": !isEditing,
"data-font": font,
"data-align": align,
"data-hastext": !isEmpty,
"data-isediting": isEditing,
"data-textwrap": !!wrap,
"data-isselected": isSelected,
style: {
justifyContent: align === "middle" || legacyAlign ? "center" : align,
alignItems: verticalAlign === "middle" ? "center" : verticalAlign,
padding,
...style
},
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
className: `${cssPrefix}-label__inner tl-text-content__wrapper`,
style: {
fontSize,
lineHeight: lineHeight.toString(),
minHeight: Math.floor(fontSize * lineHeight) + "px",
minWidth: Math.ceil(textWidth || 0),
color: labelColor,
width: textWidth ? Math.ceil(textWidth) : void 0,
height: textHeight ? Math.ceil(textHeight) : void 0
},
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `${cssPrefix} tl-text tl-text-content`, dir: "auto", children: richText && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"div",
{
className: "tl-rich-text",
"data-is-select-tool-active": selectToolActive,
dangerouslySetInnerHTML: { __html: html || "" },
onPointerDown: handlePointerDown,
"data-is-ready-for-editing": isReadyForEditing
}
) }),
(isReadyForEditing || isSelected) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_RichTextArea.RichTextArea,
{
ref: rInput,
richText,
isEditing,
shapeId,
...editableTextRest,
hasCustomTabBehavior,
handleKeyDown: handleKeyDownCustom ?? editableTextRest.handleKeyDown
}
)
]
}
)
}
);
});
function RichTextSVG({
bounds,
richText,
fontSize,
font,
align,
verticalAlign,
wrap,
labelColor,
padding,
showTextOutline = true
}) {
const editor = (0, import_editor.useEditor)();
const html = (0, import_richText.renderHtmlFromRichText)(editor, richText);
const textAlign = align === "middle" ? "center" : align === "start" ? "start" : "end";
const justifyContent = align === "middle" ? "center" : align === "start" ? "flex-start" : "flex-end";
const alignItems = verticalAlign === "middle" ? "center" : verticalAlign === "start" ? "flex-start" : "flex-end";
const wrapperStyle = {
display: "flex",
fontFamily: import_editor.DefaultFontFamilies[font],
height: `100%`,
justifyContent,
alignItems,
padding: `${padding}px`
};
const style = {
fontSize: `${fontSize}px`,
wrap: wrap ? "wrap" : "nowrap",
color: labelColor,
lineHeight: import_default_shape_constants.TEXT_PROPS.lineHeight,
textAlign,
width: "100%",
wordWrap: "break-word",
overflowWrap: "break-word",
whiteSpace: "pre-wrap",
textShadow: showTextOutline ? "var(--tl-text-outline)" : "none",
tabSize: "var(--tl-tab-size, 2)"
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"foreignObject",
{
x: bounds.minX,
y: bounds.minY,
width: bounds.w,
height: bounds.h,
className: (0, import_classnames.default)(
"tl-export-embed-styles tl-rich-text tl-rich-text-svg",
showTextOutline ? "tl-text__outline" : "tl-text__no-outline"
),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: wrapperStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { dangerouslySetInnerHTML: { __html: html }, style }) })
}
);
}
//# sourceMappingURL=RichTextLabel.js.map