UNPKG

tldraw

Version:

A tiny little drawing editor.

8 lines (7 loc) 2.47 kB
{ "version": 3, "sources": ["../../../../src/lib/ui/hooks/useRelevantStyles.ts"], "sourcesContent": ["import {\n\tDefaultColorStyle,\n\tDefaultDashStyle,\n\tDefaultFillStyle,\n\tDefaultSizeStyle,\n\tReadonlySharedStyleMap,\n\tSharedStyleMap,\n\tStyleProp,\n\tuseEditor,\n\tuseValue,\n} from '@tldraw/editor'\n\nconst selectToolStyles: readonly StyleProp<any>[] = Object.freeze([\n\tDefaultColorStyle,\n\tDefaultDashStyle,\n\tDefaultFillStyle,\n\tDefaultSizeStyle,\n])\n\n/** @public */\nexport function useRelevantStyles(stylesToCheck = selectToolStyles): ReadonlySharedStyleMap | null {\n\tconst editor = useEditor()\n\treturn useValue(\n\t\t'getRelevantStyles',\n\t\t() => {\n\t\t\tconst styles = new SharedStyleMap(editor.getSharedStyles())\n\t\t\tconst isInShapeSpecificTool = !!editor.root.getCurrent()?.shapeType\n\t\t\tconst hasShapesSelected = editor.isIn('select') && editor.getSelectedShapeIds().length > 0\n\n\t\t\tif (styles.size === 0 && editor.isIn('select') && editor.getSelectedShapeIds().length === 0) {\n\t\t\t\tfor (const style of stylesToCheck) {\n\t\t\t\t\tstyles.applyValue(style, editor.getStyleForNextShape(style))\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (isInShapeSpecificTool || hasShapesSelected || styles.size > 0) {\n\t\t\t\t// If size is 0 we may still want to return an empty styles map to allow\n\t\t\t\t// the opacity slider to show up.\n\t\t\t\t// This can happen in two situations:\n\t\t\t\t// 1. When the user is in the select tool and has multiple shapes selected but they have no supported styles (beyond opacity).\n\t\t\t\t// 2. When the user is in a shape-specific tool and the shape has no supported styles (beyond opacity obvs).\n\t\t\t\treturn styles\n\t\t\t}\n\n\t\t\treturn null\n\t\t},\n\t\t[editor]\n\t)\n}\n"], "mappings": "AAAA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,EACA;AAAA,OACM;AAEP,MAAM,mBAA8C,OAAO,OAAO;AAAA,EACjE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,CAAC;AAGM,SAAS,kBAAkB,gBAAgB,kBAAiD;AAClG,QAAM,SAAS,UAAU;AACzB,SAAO;AAAA,IACN;AAAA,IACA,MAAM;AACL,YAAM,SAAS,IAAI,eAAe,OAAO,gBAAgB,CAAC;AAC1D,YAAM,wBAAwB,CAAC,CAAC,OAAO,KAAK,WAAW,GAAG;AAC1D,YAAM,oBAAoB,OAAO,KAAK,QAAQ,KAAK,OAAO,oBAAoB,EAAE,SAAS;AAEzF,UAAI,OAAO,SAAS,KAAK,OAAO,KAAK,QAAQ,KAAK,OAAO,oBAAoB,EAAE,WAAW,GAAG;AAC5F,mBAAW,SAAS,eAAe;AAClC,iBAAO,WAAW,OAAO,OAAO,qBAAqB,KAAK,CAAC;AAAA,QAC5D;AAAA,MACD;AAEA,UAAI,yBAAyB,qBAAqB,OAAO,OAAO,GAAG;AAMlE,eAAO;AAAA,MACR;AAEA,aAAO;AAAA,IACR;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AACD;", "names": [] }