UNPKG

tldraw

Version:

A tiny little drawing editor.

8 lines (7 loc) 4.66 kB
{ "version": 3, "sources": ["../../../../src/lib/shapes/frame/frameHelpers.ts"], "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tTLFrameShape,\n\tcanonicalizeRotation,\n\tlast,\n\ttoDomPrecision,\n} from '@tldraw/editor'\nimport { TLCreateTextJsxFromSpansOpts } from '../shared/createTextJsxFromSpans'\nimport { defaultEmptyAs } from './FrameShapeUtil'\n\nexport function getFrameHeadingSide(editor: Editor, shape: TLFrameShape): 0 | 1 | 2 | 3 {\n\tconst pageRotation = canonicalizeRotation(editor.getShapePageTransform(shape.id)!.rotation())\n\tconst offsetRotation = pageRotation + Math.PI / 4\n\tconst scaledRotation = (offsetRotation * (2 / Math.PI) + 4) % 4\n\treturn Math.floor(scaledRotation) as 0 | 1 | 2 | 3\n}\n\n/**\n * Get the frame heading info (size and text) for a frame shape.\n *\n * @param editor The editor instance.\n * @param shape The frame shape.\n * @param opts The text measurement options.\n *\n * @returns The frame heading's size (as a Box) and JSX text spans.\n */\nexport function getFrameHeadingInfo(\n\teditor: Editor,\n\tshape: TLFrameShape,\n\topts: TLCreateTextJsxFromSpansOpts\n) {\n\tif (process.env.NODE_ENV === 'test') {\n\t\t// can't really measure text in tests\n\t\treturn {\n\t\t\tbox: new Box(0, -opts.height, shape.props.w, opts.height),\n\t\t\tspans: [],\n\t\t}\n\t}\n\n\tconst spans = editor.textMeasure.measureTextSpans(\n\t\tdefaultEmptyAs(shape.props.name, 'Frame') + String.fromCharCode(8203),\n\t\topts\n\t)\n\n\tconst firstSpan = spans[0]\n\tconst lastSpan = last(spans)!\n\tconst labelTextWidth = lastSpan.box.w + lastSpan.box.x - firstSpan.box.x\n\n\treturn {\n\t\tbox: new Box(0, -opts.height, labelTextWidth, opts.height),\n\t\tspans,\n\t}\n}\n\nexport function getFrameHeadingOpts(\n\tshape: TLFrameShape,\n\tcolor: string\n): TLCreateTextJsxFromSpansOpts {\n\treturn {\n\t\tfontSize: 12,\n\t\tfontFamily: 'Inter, sans-serif',\n\t\ttextAlign: 'start' as const,\n\t\twidth: shape.props.w,\n\t\theight: 32,\n\t\tpadding: 0,\n\t\tlineHeight: 1,\n\t\tfontStyle: 'normal',\n\t\tfontWeight: 'normal',\n\t\toverflow: 'truncate-ellipsis' as const,\n\t\tverticalTextAlign: 'middle' as const,\n\t\tfill: color,\n\t\toffsetY: -(32 + 2),\n\t\toffsetX: 2,\n\t}\n}\n\nexport function getFrameHeadingTranslation(\n\tshape: TLFrameShape,\n\tside: 0 | 1 | 2 | 3,\n\tisSvg: boolean\n) {\n\tconst u = isSvg ? '' : 'px'\n\tconst r = isSvg ? '' : 'deg'\n\tlet labelTranslate: string\n\tswitch (side) {\n\t\tcase 0: // top\n\t\t\tlabelTranslate = ``\n\t\t\tbreak\n\t\tcase 3: // right\n\t\t\tlabelTranslate = `translate(${toDomPrecision(shape.props.w)}${u}, 0${u}) rotate(90${r})`\n\t\t\tbreak\n\t\tcase 2: // bottom\n\t\t\tlabelTranslate = `translate(${toDomPrecision(shape.props.w)}${u}, ${toDomPrecision(\n\t\t\t\tshape.props.h\n\t\t\t)}${u}) rotate(180${r})`\n\t\t\tbreak\n\t\tcase 1: // left\n\t\t\tlabelTranslate = `translate(0${u}, ${toDomPrecision(shape.props.h)}${u}) rotate(270${r})`\n\t\t\tbreak\n\t\tdefault:\n\t\t\tthrow Error('labelSide out of bounds')\n\t}\n\n\treturn labelTranslate\n}\n"], "mappings": "AAAA;AAAA,EACC;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,SAAS,sBAAsB;AAExB,SAAS,oBAAoB,QAAgB,OAAoC;AACvF,QAAM,eAAe,qBAAqB,OAAO,sBAAsB,MAAM,EAAE,EAAG,SAAS,CAAC;AAC5F,QAAM,iBAAiB,eAAe,KAAK,KAAK;AAChD,QAAM,kBAAkB,kBAAkB,IAAI,KAAK,MAAM,KAAK;AAC9D,SAAO,KAAK,MAAM,cAA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