Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TLDrawState.ts 70KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. /* eslint-disable @typescript-eslint/ban-ts-comment */
  3. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  4. import { StateManager } from 'rko'
  5. import { Vec } from '@tldraw/vec'
  6. import {
  7. TLBoundsEventHandler,
  8. TLBoundsHandleEventHandler,
  9. TLKeyboardEventHandler,
  10. TLShapeCloneHandler,
  11. TLCanvasEventHandler,
  12. TLPageState,
  13. TLPinchEventHandler,
  14. TLPointerEventHandler,
  15. TLWheelEventHandler,
  16. Utils,
  17. TLBounds,
  18. Inputs,
  19. } from '@tldraw/core'
  20. import {
  21. FlipType,
  22. TLDrawDocument,
  23. MoveType,
  24. AlignType,
  25. StretchType,
  26. DistributeType,
  27. ShapeStyles,
  28. TLDrawShape,
  29. TLDrawShapeType,
  30. Data,
  31. Session,
  32. TLDrawStatus,
  33. SelectHistory,
  34. TLDrawPage,
  35. TLDrawBinding,
  36. GroupShape,
  37. TLDrawCommand,
  38. TLDrawUser,
  39. SessionType,
  40. ExceptFirst,
  41. ExceptFirstTwo,
  42. } from '~types'
  43. import {
  44. migrate,
  45. FileSystemHandle,
  46. loadFileHandle,
  47. openFromFileSystem,
  48. saveToFileSystem,
  49. } from './data'
  50. import { TLDR } from './TLDR'
  51. import { shapeUtils } from '~state/shapes'
  52. import { defaultStyle } from '~state/shapes/shape-styles'
  53. import * as Commands from './commands'
  54. import { ArgsOfType, getSession } from './sessions'
  55. import { createTools, ToolType } from './tools'
  56. import type { BaseTool } from './tools/BaseTool'
  57. import { USER_COLORS, FIT_TO_SCREEN_PADDING } from '~constants'
  58. const uuid = Utils.uniqueId()
  59. export class TLDrawState extends StateManager<Data> {
  60. private _onMount?: (tlstate: TLDrawState) => void
  61. private _onChange?: (tlstate: TLDrawState, data: Data, reason: string) => void
  62. private _onUserChange?: (tlstate: TLDrawState, user: TLDrawUser) => void
  63. readOnly = false
  64. inputs?: Inputs
  65. selectHistory: SelectHistory = {
  66. stack: [[]],
  67. pointer: 0,
  68. }
  69. clipboard?: {
  70. shapes: TLDrawShape[]
  71. bindings: TLDrawBinding[]
  72. }
  73. tools = createTools(this)
  74. currentTool: BaseTool = this.tools.select
  75. session?: Session
  76. isCreating = false
  77. // The editor's bounding client rect
  78. bounds: TLBounds = {
  79. minX: 0,
  80. minY: 0,
  81. maxX: 640,
  82. maxY: 480,
  83. width: 640,
  84. height: 480,
  85. }
  86. // The most recent pointer location
  87. pointerPoint: number[] = [0, 0]
  88. private pasteInfo = {
  89. center: [0, 0],
  90. offset: [0, 0],
  91. }
  92. fileSystemHandle: FileSystemHandle | null = null
  93. isDirty = false
  94. constructor(
  95. id?: string,
  96. onMount?: (tlstate: TLDrawState) => void,
  97. onChange?: (tlstate: TLDrawState, data: Data, reason: string) => void,
  98. onUserChange?: (tlstate: TLDrawState, user: TLDrawUser) => void
  99. ) {
  100. super(TLDrawState.defaultState, id, TLDrawState.version, (prev, next, prevVersion) => {
  101. return {
  102. ...next,
  103. document: migrate(
  104. { ...next.document, ...prev.document, version: prevVersion },
  105. TLDrawState.version
  106. ),
  107. }
  108. })
  109. this.loadDocument(this.document)
  110. this.patchState({ document: migrate(this.document, TLDrawState.version) })
  111. loadFileHandle().then((fileHandle) => {
  112. this.fileSystemHandle = fileHandle
  113. })
  114. this._onChange = onChange
  115. this._onMount = onMount
  116. this._onUserChange = onUserChange
  117. this.session = undefined
  118. }
  119. /* -------------------- Internal -------------------- */
  120. onReady = () => {
  121. try {
  122. this.patchState({
  123. appState: {
  124. status: TLDrawStatus.Idle,
  125. },
  126. document: migrate(this.document, TLDrawState.version),
  127. })
  128. } catch (e) {
  129. console.error('The data appears to be corrupted. Resetting!', e)
  130. localStorage.setItem(this.document.id + '_corrupted', JSON.stringify(this.document))
  131. this.patchState({
  132. ...TLDrawState.defaultState,
  133. appState: {
  134. ...TLDrawState.defaultState.appState,
  135. status: TLDrawStatus.Idle,
  136. },
  137. })
  138. }
  139. this.persist()
  140. this._onMount?.(this)
  141. }
  142. /**
  143. * Cleanup the state after each state change.
  144. * @param state The new state
  145. * @param prev The previous state
  146. * @protected
  147. * @returns The final state
  148. */
  149. protected cleanup = (state: Data, prev: Data): Data => {
  150. const data = { ...state }
  151. // Remove deleted shapes and bindings (in Commands, these will be set to undefined)
  152. if (data.document !== prev.document) {
  153. Object.entries(data.document.pages).forEach(([pageId, page]) => {
  154. if (page === undefined) {
  155. // If page is undefined, delete the page and pagestate
  156. delete data.document.pages[pageId]
  157. delete data.document.pageStates[pageId]
  158. return
  159. }
  160. const prevPage = prev.document.pages[pageId]
  161. if (!prevPage || page.shapes !== prevPage.shapes || page.bindings !== prevPage.bindings) {
  162. page.shapes = { ...page.shapes }
  163. page.bindings = { ...page.bindings }
  164. const groupsToUpdate = new Set<GroupShape>()
  165. // If shape is undefined, delete the shape
  166. Object.keys(page.shapes).forEach((id) => {
  167. const shape = page.shapes[id]
  168. let parentId: string
  169. if (!shape) {
  170. parentId = prevPage.shapes[id]?.parentId
  171. delete page.shapes[id]
  172. } else {
  173. parentId = shape.parentId
  174. }
  175. // If the shape is the child of a group, then update the group
  176. // (unless the group is being deleted too)
  177. if (parentId && parentId !== pageId) {
  178. const group = page.shapes[parentId]
  179. if (group !== undefined) {
  180. groupsToUpdate.add(page.shapes[parentId] as GroupShape)
  181. }
  182. }
  183. })
  184. // If binding is undefined, delete the binding
  185. Object.keys(page.bindings).forEach((id) => {
  186. if (!page.bindings[id]) {
  187. delete page.bindings[id]
  188. }
  189. })
  190. // Find which shapes have changed
  191. const changedShapeIds = Object.values(page.shapes)
  192. .filter((shape) => prevPage?.shapes[shape.id] !== shape)
  193. .map((shape) => shape.id)
  194. data.document.pages[pageId] = page
  195. // Get bindings related to the changed shapes
  196. const bindingsToUpdate = TLDR.getRelatedBindings(data, changedShapeIds, pageId)
  197. // Update all of the bindings we've just collected
  198. bindingsToUpdate.forEach((binding) => {
  199. if (!page.bindings[binding.id]) {
  200. return
  201. }
  202. const toShape = page.shapes[binding.toId]
  203. const fromShape = page.shapes[binding.fromId]
  204. const toUtils = TLDR.getShapeUtils(toShape)
  205. const fromUtils = TLDR.getShapeUtils(fromShape)
  206. // We only need to update the binding's "from" shape
  207. const fromDelta = fromUtils.onBindingChange?.(
  208. fromShape,
  209. binding,
  210. toShape,
  211. toUtils.getBounds(toShape),
  212. toUtils.getCenter(toShape)
  213. )
  214. if (fromDelta) {
  215. const nextShape = {
  216. ...fromShape,
  217. ...fromDelta,
  218. } as TLDrawShape
  219. page.shapes[fromShape.id] = nextShape
  220. }
  221. })
  222. groupsToUpdate.forEach((group) => {
  223. if (!group) throw Error('no group!')
  224. const children = group.children.filter((id) => page.shapes[id] !== undefined)
  225. const commonBounds = Utils.getCommonBounds(
  226. children
  227. .map((id) => page.shapes[id])
  228. .filter(Boolean)
  229. .map((shape) => TLDR.getRotatedBounds(shape))
  230. )
  231. page.shapes[group.id] = {
  232. ...group,
  233. point: [commonBounds.minX, commonBounds.minY],
  234. size: [commonBounds.width, commonBounds.height],
  235. children,
  236. }
  237. })
  238. }
  239. // Clean up page state, preventing hovers on deleted shapes
  240. const nextPageState: TLPageState = {
  241. ...data.document.pageStates[pageId],
  242. }
  243. if (!nextPageState.brush) {
  244. delete nextPageState.brush
  245. }
  246. if (nextPageState.hoveredId && !page.shapes[nextPageState.hoveredId]) {
  247. delete nextPageState.hoveredId
  248. }
  249. if (nextPageState.bindingId && !page.bindings[nextPageState.bindingId]) {
  250. console.warn('Could not find the binding binding!', pageId)
  251. delete nextPageState.bindingId
  252. }
  253. if (nextPageState.editingId && !page.shapes[nextPageState.editingId]) {
  254. console.warn('Could not find the editing shape!')
  255. delete nextPageState.editingId
  256. }
  257. data.document.pageStates[pageId] = nextPageState
  258. })
  259. }
  260. const currentPageId = data.appState.currentPageId
  261. const currentPageState = data.document.pageStates[currentPageId]
  262. if (data.room && data.room !== prev.room) {
  263. const room = { ...data.room, users: { ...data.room.users } }
  264. // Remove any exited users
  265. if (prev.room) {
  266. Object.values(prev.room.users)
  267. .filter(Boolean)
  268. .forEach((user) => {
  269. if (room.users[user.id] === undefined) {
  270. delete room.users[user.id]
  271. }
  272. })
  273. }
  274. data.room = room
  275. }
  276. if (data.room) {
  277. data.room.users[data.room.userId] = {
  278. ...data.room.users[data.room.userId],
  279. point: this.pointerPoint,
  280. selectedIds: currentPageState.selectedIds,
  281. }
  282. }
  283. // Apply selected style change, if any
  284. const newSelectedStyle = TLDR.getSelectedStyle(data, currentPageId)
  285. if (newSelectedStyle) {
  286. data.appState = {
  287. ...data.appState,
  288. selectedStyle: newSelectedStyle,
  289. }
  290. }
  291. // Temporary block on editing pages while in readonly mode.
  292. // This is a broad solution but not a very good one: the UX
  293. // for interacting with a readOnly document will be more nuanced.
  294. if (this.readOnly) {
  295. data.document.pages = prev.document.pages
  296. }
  297. return data
  298. }
  299. /**
  300. * Clear the selection history after each new command, undo or redo.
  301. * @param state
  302. * @param id
  303. */
  304. protected onStateDidChange = (state: Data, id: string): void => {
  305. if (!id.startsWith('patch')) {
  306. if (!id.startsWith('replace')) {
  307. // If we've changed the undo stack, then the file is out of
  308. // sync with any saved version on the file system.
  309. this.isDirty = true
  310. }
  311. this.clearSelectHistory()
  312. }
  313. if (id.startsWith('undo') || id.startsWith('redo')) {
  314. Session.cache.selectedIds = [...this.selectedIds]
  315. }
  316. this._onChange?.(this, state, id)
  317. }
  318. /**
  319. * Set the current status.
  320. * @param status The new status to set.
  321. * @private
  322. * @returns
  323. */
  324. setStatus(status: string) {
  325. return this.patchState(
  326. {
  327. appState: { status },
  328. },
  329. `set_status:${status}`
  330. )
  331. }
  332. /**
  333. * Update the bounding box when the renderer's bounds change.
  334. * @param bounds
  335. */
  336. updateBounds = (bounds: TLBounds) => {
  337. this.bounds = { ...bounds }
  338. if (this.session) {
  339. this.session.updateViewport(this.viewport)
  340. this.session.update(this.state, this.pointerPoint, false, false, false)
  341. }
  342. }
  343. /**
  344. * Set or clear the editing id
  345. * @param id [string]
  346. */
  347. setEditingId = (id?: string) => {
  348. this.patchState(
  349. {
  350. document: {
  351. pageStates: {
  352. [this.currentPageId]: {
  353. editingId: id,
  354. },
  355. },
  356. },
  357. },
  358. `set_editing_id`
  359. )
  360. }
  361. /**
  362. * Set or clear the hovered id
  363. * @param id [string]
  364. */
  365. setHoveredId = (id?: string) => {
  366. this.patchState(
  367. {
  368. document: {
  369. pageStates: {
  370. [this.currentPageId]: {
  371. hoveredId: id,
  372. },
  373. },
  374. },
  375. },
  376. `set_hovered_id`
  377. )
  378. }
  379. /* -------------------------------------------------- */
  380. /* Settings & UI */
  381. /* -------------------------------------------------- */
  382. /**
  383. * Set a setting.
  384. */
  385. setSetting = <T extends keyof Data['settings'], V extends Data['settings'][T]>(
  386. name: T,
  387. value: V | ((value: V) => V)
  388. ): this => {
  389. if (this.session) return this
  390. return this.patchState(
  391. {
  392. settings: {
  393. [name]: typeof value === 'function' ? value(this.state.settings[name] as V) : value,
  394. },
  395. },
  396. `settings:${name}`
  397. )
  398. }
  399. /**
  400. * Toggle pen mode.
  401. */
  402. toggleFocusMode = (): this => {
  403. if (this.session) return this
  404. return this.patchState(
  405. {
  406. settings: {
  407. isFocusMode: !this.state.settings.isFocusMode,
  408. },
  409. },
  410. `settings:toggled_focus_mode`
  411. )
  412. }
  413. /**
  414. * Toggle pen mode.
  415. */
  416. togglePenMode = (): this => {
  417. if (this.session) return this
  418. return this.patchState(
  419. {
  420. settings: {
  421. isPenMode: !this.state.settings.isPenMode,
  422. },
  423. },
  424. `settings:toggled_pen_mode`
  425. )
  426. }
  427. /**
  428. * Toggle dark mode.
  429. */
  430. toggleDarkMode = (): this => {
  431. if (this.session) return this
  432. this.patchState(
  433. { settings: { isDarkMode: !this.state.settings.isDarkMode } },
  434. `settings:toggled_dark_mode`
  435. )
  436. this.persist()
  437. return this
  438. }
  439. /**
  440. * Toggle zoom snap.
  441. */
  442. toggleZoomSnap = () => {
  443. if (this.session) return this
  444. this.patchState(
  445. { settings: { isZoomSnap: !this.state.settings.isZoomSnap } },
  446. `settings:toggled_zoom_snap`
  447. )
  448. this.persist()
  449. return this
  450. }
  451. /**
  452. * Toggle debug mode.
  453. */
  454. toggleDebugMode = () => {
  455. if (this.session) return this
  456. this.patchState(
  457. { settings: { isDebugMode: !this.state.settings.isDebugMode } },
  458. `settings:toggled_debug`
  459. )
  460. this.persist()
  461. return this
  462. }
  463. /**
  464. * Toggle the style panel.
  465. */
  466. toggleStylePanel = (): this => {
  467. if (this.session) return this
  468. this.patchState(
  469. { appState: { isStyleOpen: !this.appState.isStyleOpen } },
  470. 'ui:toggled_style_panel'
  471. )
  472. this.persist()
  473. return this
  474. }
  475. /**
  476. * Select a tool.
  477. * @param tool The tool to select, or "select".
  478. */
  479. selectTool = (type: ToolType): this => {
  480. if (this.session) return this
  481. const tool = this.tools[type]
  482. if (tool === this.currentTool) return this
  483. this.currentTool.onExit()
  484. this.currentTool = tool
  485. this.currentTool.onEnter()
  486. return this.patchState(
  487. {
  488. appState: {
  489. activeTool: type,
  490. },
  491. },
  492. `selected_tool:${type}`
  493. )
  494. }
  495. /**
  496. * Toggle the tool lock option.
  497. */
  498. toggleToolLock = (): this => {
  499. if (this.session) return this
  500. return this.patchState(
  501. {
  502. appState: {
  503. isToolLocked: !this.appState.isToolLocked,
  504. },
  505. },
  506. `toggled_tool_lock`
  507. )
  508. }
  509. /* -------------------------------------------------- */
  510. /* Document */
  511. /* -------------------------------------------------- */
  512. /**
  513. * Reset the document to a blank state.
  514. */
  515. resetDocument = (): this => {
  516. if (this.session) return this
  517. this.session = undefined
  518. this.pasteInfo.offset = [0, 0]
  519. this.tools = createTools(this)
  520. this.currentTool = this.tools.select
  521. this.resetHistory()
  522. .clearSelectHistory()
  523. .loadDocument(migrate(TLDrawState.defaultDocument, TLDrawState.version))
  524. .persist()
  525. return this
  526. }
  527. /**
  528. *
  529. * @param document
  530. */
  531. updateUsers = (users: TLDrawUser[], isOwnUpdate = false) => {
  532. this.patchState(
  533. {
  534. room: {
  535. users: Object.fromEntries(users.map((user) => [user.id, user])),
  536. },
  537. },
  538. isOwnUpdate ? 'room:self:update' : 'room:user:update'
  539. )
  540. }
  541. removeUser = (userId: string) => {
  542. this.patchState({
  543. room: {
  544. users: {
  545. [userId]: undefined,
  546. },
  547. },
  548. })
  549. }
  550. /**
  551. * Merge a new document patch into the current document.
  552. * @param document
  553. */
  554. mergeDocument = (document: TLDrawDocument): this => {
  555. // If it's a new document, do a full change.
  556. if (this.document.id !== document.id) {
  557. this.replaceState({
  558. ...this.state,
  559. appState: {
  560. ...this.appState,
  561. currentPageId: Object.keys(document.pages)[0],
  562. },
  563. document: migrate(document, TLDrawState.version),
  564. })
  565. return this
  566. }
  567. // Have we deleted any pages? If so, drop everything and change
  568. // to the first page. This is an edge case.
  569. const currentPageStates = { ...this.document.pageStates }
  570. // Update the app state's current page id if needed
  571. const nextAppState = {
  572. ...this.appState,
  573. currentPageId: document.pages[this.currentPageId]
  574. ? this.currentPageId
  575. : Object.keys(document.pages)[0],
  576. pages: Object.values(document.pages).map((page, i) => ({
  577. id: page.id,
  578. name: page.name,
  579. childIndex: page.childIndex || i,
  580. })),
  581. }
  582. // Reset the history (for now)
  583. this.resetHistory()
  584. Object.keys(this.document.pages).forEach((pageId) => {
  585. if (!document.pages[pageId]) {
  586. if (pageId === this.appState.currentPageId) {
  587. this.cancelSession()
  588. this.deselectAll()
  589. }
  590. currentPageStates[pageId] = undefined as unknown as TLPageState
  591. }
  592. })
  593. // Don't allow the selected ids to be deleted during a session—if
  594. // they've been removed, put them back in the client's document.
  595. if (this.session) {
  596. this.selectedIds
  597. .filter((id) => !document.pages[this.currentPageId].shapes[id])
  598. .forEach((id) => (document.pages[this.currentPageId].shapes[id] = this.page.shapes[id]))
  599. }
  600. // For other pages, remove any selected ids that were deleted.
  601. Object.entries(currentPageStates).forEach(([pageId, pageState]) => {
  602. pageState.selectedIds = pageState.selectedIds.filter(
  603. (id) => !!document.pages[pageId].shapes[id]
  604. )
  605. })
  606. // If the user is currently creating a shape (ie drawing), then put that
  607. // shape back onto the page for the client.
  608. const { editingId } = this.pageState
  609. if (editingId) {
  610. console.warn('A change occured while creating a shape')
  611. if (!editingId) throw Error('Huh?')
  612. document.pages[this.currentPageId].shapes[editingId] = this.page.shapes[editingId]
  613. currentPageStates[this.currentPageId].selectedIds = [editingId]
  614. }
  615. return this.replaceState(
  616. {
  617. ...this.state,
  618. appState: nextAppState,
  619. document: {
  620. ...migrate(document, TLDrawState.version),
  621. pageStates: currentPageStates,
  622. },
  623. },
  624. 'merge'
  625. )
  626. }
  627. /**
  628. * Update the current document.
  629. * @param document
  630. */
  631. updateDocument = (document: TLDrawDocument, reason = 'updated_document'): this => {
  632. const prevState = this.state
  633. const nextState = { ...prevState, document: { ...prevState.document } }
  634. if (!document.pages[this.currentPageId]) {
  635. nextState.appState = {
  636. ...prevState.appState,
  637. currentPageId: Object.keys(document.pages)[0],
  638. }
  639. }
  640. let i = 1
  641. for (const nextPage of Object.values(document.pages)) {
  642. if (nextPage !== prevState.document.pages[nextPage.id]) {
  643. nextState.document.pages[nextPage.id] = nextPage
  644. if (!nextPage.name) {
  645. nextState.document.pages[nextPage.id].name = `Page ${i + 1}`
  646. i++
  647. }
  648. }
  649. }
  650. for (const nextPageState of Object.values(document.pageStates)) {
  651. if (nextPageState !== prevState.document.pageStates[nextPageState.id]) {
  652. nextState.document.pageStates[nextPageState.id] = nextPageState
  653. const nextPage = document.pages[nextPageState.id]
  654. const keysToCheck = ['bindingId', 'editingId', 'hoveredId', 'pointedId'] as const
  655. for (const key of keysToCheck) {
  656. if (!nextPage.shapes[key]) {
  657. nextPageState[key] = undefined
  658. }
  659. }
  660. nextPageState.selectedIds = nextPageState.selectedIds.filter(
  661. (id) => !!document.pages[nextPage.id].shapes[id]
  662. )
  663. }
  664. }
  665. return this.replaceState(nextState, `${reason}:${document.id}`)
  666. }
  667. /**
  668. * Load a fresh room into the state.
  669. * @param roomId
  670. */
  671. loadRoom = (roomId: string) => {
  672. this.patchState({
  673. room: {
  674. id: roomId,
  675. userId: uuid,
  676. users: {
  677. [uuid]: {
  678. id: uuid,
  679. color: USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)],
  680. point: [100, 100],
  681. selectedIds: [],
  682. activeShapes: [],
  683. },
  684. },
  685. },
  686. })
  687. }
  688. /**
  689. * Load a new document.
  690. * @param document The document to load
  691. */
  692. loadDocument = (document: TLDrawDocument): this => {
  693. this.deselectAll()
  694. this.resetHistory()
  695. this.clearSelectHistory()
  696. this.session = undefined
  697. return this.replaceState(
  698. {
  699. ...TLDrawState.defaultState,
  700. document: migrate(document, TLDrawState.version),
  701. appState: {
  702. ...TLDrawState.defaultState.appState,
  703. currentPageId: Object.keys(document.pages)[0],
  704. },
  705. },
  706. 'loaded_document'
  707. )
  708. }
  709. // Should we move this to the app layer? onSave, onSaveAs, etc?
  710. /**
  711. * Create a new project.
  712. */
  713. newProject = () => {
  714. if (!this.isLocal) return
  715. this.fileSystemHandle = null
  716. this.resetDocument()
  717. }
  718. /**
  719. * Save the current project.
  720. */
  721. saveProject = async () => {
  722. if (this.readOnly) return
  723. try {
  724. const fileHandle = await saveToFileSystem(this.document, this.fileSystemHandle)
  725. this.fileSystemHandle = fileHandle
  726. this.persist()
  727. this.isDirty = false
  728. } catch (e: any) {
  729. // Likely cancelled
  730. console.error(e.message)
  731. }
  732. return this
  733. }
  734. /**
  735. * Save the current project as a new file.
  736. */
  737. saveProjectAs = async () => {
  738. try {
  739. const fileHandle = await saveToFileSystem(this.document, null)
  740. this.fileSystemHandle = fileHandle
  741. this.persist()
  742. this.isDirty = false
  743. } catch (e: any) {
  744. // Likely cancelled
  745. console.error(e.message)
  746. }
  747. return this
  748. }
  749. /**
  750. * Load a project from the filesystem.
  751. * @todo
  752. */
  753. openProject = async () => {
  754. if (!this.isLocal) return
  755. try {
  756. const result = await openFromFileSystem()
  757. if (!result) {
  758. throw Error()
  759. }
  760. const { fileHandle, document } = result
  761. this.loadDocument(document)
  762. this.fileSystemHandle = fileHandle
  763. this.zoomToFit()
  764. this.persist()
  765. } catch (e) {
  766. console.error(e)
  767. } finally {
  768. this.persist()
  769. }
  770. }
  771. /**
  772. * Sign out of the current account.
  773. * Should move to the www layer.
  774. * @todo
  775. */
  776. signOut = () => {
  777. // todo
  778. }
  779. /* -------------------- Getters --------------------- */
  780. /**
  781. * Get the current app state.
  782. */
  783. getAppState = (): Data['appState'] => {
  784. return this.appState
  785. }
  786. /**
  787. * Get a page.
  788. * @param pageId (optional) The page's id.
  789. */
  790. getPage = (pageId = this.currentPageId): TLDrawPage => {
  791. return TLDR.getPage(this.state, pageId || this.currentPageId)
  792. }
  793. /**
  794. * Get the shapes (as an array) from a given page.
  795. * @param pageId (optional) The page's id.
  796. */
  797. getShapes = (pageId = this.currentPageId): TLDrawShape[] => {
  798. return TLDR.getShapes(this.state, pageId || this.currentPageId)
  799. }
  800. /**
  801. * Get the bindings from a given page.
  802. * @param pageId (optional) The page's id.
  803. */
  804. getBindings = (pageId = this.currentPageId): TLDrawBinding[] => {
  805. return TLDR.getBindings(this.state, pageId || this.currentPageId)
  806. }
  807. /**
  808. * Get a shape from a given page.
  809. * @param id The shape's id.
  810. * @param pageId (optional) The page's id.
  811. */
  812. getShape = <T extends TLDrawShape = TLDrawShape>(id: string, pageId = this.currentPageId): T => {
  813. return TLDR.getShape<T>(this.state, id, pageId)
  814. }
  815. /**
  816. * Get the bounds of a shape on a given page.
  817. * @param id The shape's id.
  818. * @param pageId (optional) The page's id.
  819. */
  820. getShapeBounds = (id: string, pageId = this.currentPageId): TLBounds => {
  821. return TLDR.getBounds(this.getShape(id, pageId))
  822. }
  823. greet() {
  824. return 'hello'
  825. }
  826. /**
  827. * Get a binding from a given page.
  828. * @param id The binding's id.
  829. * @param pageId (optional) The page's id.
  830. */
  831. getBinding = (id: string, pageId = this.currentPageId): TLDrawBinding => {
  832. return TLDR.getBinding(this.state, id, pageId)
  833. }
  834. /**
  835. * Get the page state for a given page.
  836. * @param pageId (optional) The page's id.
  837. */
  838. getPageState = (pageId = this.currentPageId): TLPageState => {
  839. return TLDR.getPageState(this.state, pageId || this.currentPageId)
  840. }
  841. /**
  842. * Turn a screen point into a point on the page.
  843. * @param point The screen point
  844. * @param pageId (optional) The page to use
  845. */
  846. getPagePoint = (point: number[], pageId = this.currentPageId): number[] => {
  847. const { camera } = this.getPageState(pageId)
  848. return Vec.sub(Vec.div(point, camera.zoom), camera.point)
  849. }
  850. /**
  851. * Get the current undo/redo stack.
  852. */
  853. get history() {
  854. return this.stack.slice(0, this.pointer + 1)
  855. }
  856. /**
  857. * Replace the current history stack.
  858. */
  859. set history(commands: TLDrawCommand[]) {
  860. this.replaceHistory(commands)
  861. }
  862. /**
  863. * The current document.
  864. */
  865. get document(): TLDrawDocument {
  866. return this.state.document
  867. }
  868. /**
  869. * The current app state.
  870. */
  871. get appState(): Data['appState'] {
  872. return this.state.appState
  873. }
  874. /**
  875. * The current page id.
  876. */
  877. get currentPageId(): string {
  878. return this.state.appState.currentPageId
  879. }
  880. /**
  881. * The current page.
  882. */
  883. get page(): TLDrawPage {
  884. return this.state.document.pages[this.currentPageId]
  885. }
  886. /**
  887. * The current page's shapes (as an array).
  888. */
  889. get shapes(): TLDrawShape[] {
  890. return Object.values(this.page.shapes)
  891. }
  892. /**
  893. * The current page's bindings.
  894. */
  895. get bindings(): TLDrawBinding[] {
  896. return Object.values(this.page.bindings)
  897. }
  898. /**
  899. * The current page's state.
  900. */
  901. get pageState(): TLPageState {
  902. return this.state.document.pageStates[this.currentPageId]
  903. }
  904. /**
  905. * The page's current selected ids.
  906. */
  907. get selectedIds(): string[] {
  908. return this.pageState.selectedIds
  909. }
  910. /* -------------------------------------------------- */
  911. /* Pages */
  912. /* -------------------------------------------------- */
  913. /**
  914. * Create a new page.
  915. * @param pageId (optional) The new page's id.
  916. */
  917. createPage = (id?: string): this => {
  918. return this.setState(
  919. Commands.createPage(this.state, [-this.bounds.width / 2, -this.bounds.height / 2], id)
  920. )
  921. }
  922. /**
  923. * Change the current page.
  924. * @param pageId The new current page's id.
  925. */
  926. changePage = (pageId: string): this => {
  927. return this.setState(Commands.changePage(this.state, pageId))
  928. }
  929. /**
  930. * Rename a page.
  931. * @param pageId The id of the page to rename.
  932. * @param name The page's new name
  933. */
  934. renamePage = (pageId: string, name: string): this => {
  935. return this.setState(Commands.renamePage(this.state, pageId, name))
  936. }
  937. /**
  938. * Duplicate a page.
  939. * @param pageId The id of the page to duplicate.
  940. */
  941. duplicatePage = (pageId: string): this => {
  942. if (this.readOnly) return this
  943. return this.setState(
  944. Commands.duplicatePage(this.state, [-this.bounds.width / 2, -this.bounds.height / 2], pageId)
  945. )
  946. }
  947. /**
  948. * Delete a page.
  949. * @param pageId The id of the page to delete.
  950. */
  951. deletePage = (pageId?: string): this => {
  952. if (this.readOnly) return this
  953. if (Object.values(this.document.pages).length <= 1) return this
  954. return this.setState(Commands.deletePage(this.state, pageId ? pageId : this.currentPageId))
  955. }
  956. /* -------------------------------------------------- */
  957. /* Clipboard */
  958. /* -------------------------------------------------- */
  959. /**
  960. * Copy one or more shapes to the clipboard.
  961. * @param ids The ids of the shapes to copy.
  962. */
  963. copy = (ids = this.selectedIds): this => {
  964. const copyingShapeIds = ids.flatMap((id) =>
  965. TLDR.getDocumentBranch(this.state, id, this.currentPageId)
  966. )
  967. const copyingShapes = copyingShapeIds.map((id) =>
  968. Utils.deepClone(this.getShape(id, this.currentPageId))
  969. )
  970. if (copyingShapes.length === 0) return this
  971. const copyingBindings: TLDrawBinding[] = Object.values(this.page.bindings).filter(
  972. (binding) =>
  973. copyingShapeIds.includes(binding.fromId) && copyingShapeIds.includes(binding.toId)
  974. )
  975. this.clipboard = {
  976. shapes: copyingShapes,
  977. bindings: copyingBindings,
  978. }
  979. try {
  980. const text = JSON.stringify({
  981. type: 'tldr/clipboard',
  982. shapes: copyingShapes,
  983. bindings: copyingBindings,
  984. })
  985. navigator.clipboard.writeText(text).then(
  986. () => {
  987. // success
  988. },
  989. () => {
  990. // failure
  991. }
  992. )
  993. } catch (e) {
  994. // Browser does not support copying to clipboard
  995. }
  996. this.pasteInfo.offset = [0, 0]
  997. this.pasteInfo.center = [0, 0]
  998. return this
  999. }
  1000. /**
  1001. * Paste shapes (or text) from clipboard to a certain point.
  1002. * @param point
  1003. */
  1004. paste = (point?: number[]) => {
  1005. if (this.readOnly) return
  1006. const pasteInCurrentPage = (shapes: TLDrawShape[], bindings: TLDrawBinding[]) => {
  1007. const idsMap: Record<string, string> = {}
  1008. shapes.forEach((shape) => (idsMap[shape.id] = Utils.uniqueId()))
  1009. bindings.forEach((binding) => (idsMap[binding.id] = Utils.uniqueId()))
  1010. let startIndex = TLDR.getTopChildIndex(this.state, this.currentPageId)
  1011. const shapesToPaste = shapes
  1012. .sort((a, b) => a.childIndex - b.childIndex)
  1013. .map((shape) => {
  1014. const parentShapeId = idsMap[shape.parentId]
  1015. const copy = {
  1016. ...shape,
  1017. id: idsMap[shape.id],
  1018. parentId: parentShapeId || this.currentPageId,
  1019. }
  1020. if (shape.children) {
  1021. copy.children = shape.children.map((id) => idsMap[id])
  1022. }
  1023. if (!parentShapeId) {
  1024. copy.childIndex = startIndex
  1025. startIndex++
  1026. }
  1027. if (copy.handles) {
  1028. Object.values(copy.handles).forEach((handle) => {
  1029. if (handle.bindingId) {
  1030. handle.bindingId = idsMap[handle.bindingId]
  1031. }
  1032. })
  1033. }
  1034. return copy
  1035. })
  1036. const bindingsToPaste = bindings.map((binding) => ({
  1037. ...binding,
  1038. id: idsMap[binding.id],
  1039. toId: idsMap[binding.toId],
  1040. fromId: idsMap[binding.fromId],
  1041. }))
  1042. const commonBounds = Utils.getCommonBounds(shapesToPaste.map(TLDR.getBounds))
  1043. let center = Vec.round(this.getPagePoint(point || this.centerPoint))
  1044. if (
  1045. Vec.dist(center, this.pasteInfo.center) < 2 ||
  1046. Vec.dist(center, Vec.round(Utils.getBoundsCenter(commonBounds))) < 2
  1047. ) {
  1048. center = Vec.add(center, this.pasteInfo.offset)
  1049. this.pasteInfo.offset = Vec.add(this.pasteInfo.offset, [
  1050. this.state.settings.nudgeDistanceLarge,
  1051. this.state.settings.nudgeDistanceLarge,
  1052. ])
  1053. } else {
  1054. this.pasteInfo.center = center
  1055. this.pasteInfo.offset = [0, 0]
  1056. }
  1057. const centeredBounds = Utils.centerBounds(commonBounds, center)
  1058. const delta = Vec.sub(
  1059. Utils.getBoundsCenter(centeredBounds),
  1060. Utils.getBoundsCenter(commonBounds)
  1061. )
  1062. this.create(
  1063. shapesToPaste.map((shape) =>
  1064. TLDR.getShapeUtils(shape.type).create({
  1065. ...shape,
  1066. point: Vec.round(Vec.add(shape.point, delta)),
  1067. parentId: shape.parentId || this.currentPageId,
  1068. })
  1069. ),
  1070. bindingsToPaste
  1071. )
  1072. }
  1073. try {
  1074. if (!('clipboard' in navigator && navigator.clipboard.readText)) {
  1075. throw Error('This browser does not support the clipboard API.')
  1076. }
  1077. navigator.clipboard.readText().then((result) => {
  1078. try {
  1079. const data: { type: string; shapes: TLDrawShape[]; bindings: TLDrawBinding[] } =
  1080. JSON.parse(result)
  1081. if (data.type !== 'tldr/clipboard') {
  1082. throw Error('The pasted string was not from the tldraw clipboard.')
  1083. }
  1084. pasteInCurrentPage(data.shapes, data.bindings)
  1085. } catch (e) {
  1086. console.warn(e)
  1087. const shapeId = Utils.uniqueId()
  1088. this.createShapes({
  1089. id: shapeId,
  1090. type: TLDrawShapeType.Text,
  1091. parentId: this.appState.currentPageId,
  1092. text: result,
  1093. point: this.getPagePoint(this.centerPoint, this.currentPageId),
  1094. style: { ...this.appState.currentStyle },
  1095. })
  1096. this.select(shapeId)
  1097. }
  1098. })
  1099. } catch (e) {
  1100. // Navigator does not support clipboard. Note that this fallback will
  1101. // not support pasting from one document to another.
  1102. if (this.clipboard) {
  1103. pasteInCurrentPage(this.clipboard.shapes, this.clipboard.bindings)
  1104. }
  1105. }
  1106. return this
  1107. }
  1108. /**
  1109. * Copy one or more shapes as SVG.
  1110. * @param ids The ids of the shapes to copy.
  1111. * @param pageId The page from which to copy the shapes.
  1112. * @returns A string containing the JSON.
  1113. */
  1114. copySvg = (ids = this.selectedIds, pageId = this.currentPageId) => {
  1115. if (ids.length === 0) return
  1116. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  1117. // const idsToCopy = ids.flatMap((id) => TLDR.getDocumentBranch(this.state, id, pageId))
  1118. const shapes = ids.map((id) => this.getShape(id, pageId))
  1119. function getSvgElementForShape(shape: TLDrawShape) {
  1120. const elm = document.getElementById(shape.id + '_svg')
  1121. if (!elm) return
  1122. // TODO: Create SVG elements for text
  1123. const element = elm?.cloneNode(true) as SVGElement
  1124. const bounds = TLDR.getShapeUtils(shape).getBounds(shape)
  1125. element.setAttribute(
  1126. 'transform',
  1127. `translate(${shape.point[0]}, ${shape.point[1]}) rotate(${
  1128. ((shape.rotation || 0) * 180) / Math.PI
  1129. }, ${bounds.width / 2}, ${bounds.height / 2})`
  1130. )
  1131. return element
  1132. }
  1133. shapes.forEach((shape) => {
  1134. if (shape.children?.length) {
  1135. // Create a group <g> element for shape
  1136. const g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
  1137. // Get the shape's children as elements
  1138. shape.children
  1139. .map((childId) => this.getShape(childId, pageId))
  1140. .map(getSvgElementForShape)
  1141. .filter(Boolean)
  1142. .forEach((element) => g.appendChild(element!))
  1143. // Add the group element to the SVG
  1144. svg.appendChild(g)
  1145. return
  1146. }
  1147. const element = getSvgElementForShape(shape)
  1148. if (element) {
  1149. svg.appendChild(element)
  1150. }
  1151. })
  1152. const bounds = Utils.getCommonBounds(shapes.map(TLDR.getRotatedBounds))
  1153. const padding = 16
  1154. // Resize the element to the bounding box
  1155. svg.setAttribute(
  1156. 'viewBox',
  1157. [
  1158. bounds.minX - padding,
  1159. bounds.minY - padding,
  1160. bounds.width + padding * 2,
  1161. bounds.height + padding * 2,
  1162. ].join(' ')
  1163. )
  1164. svg.setAttribute('width', String(bounds.width))
  1165. svg.setAttribute('height', String(bounds.height))
  1166. const s = new XMLSerializer()
  1167. const svgString = s
  1168. .serializeToString(svg)
  1169. .replaceAll('&#10; ', '')
  1170. .replaceAll(/((\s|")[0-9]*\.[0-9]{2})([0-9]*)(\b|"|\))/g, '$1')
  1171. TLDR.copyStringToClipboard(svgString)
  1172. return svgString
  1173. }
  1174. /**
  1175. * Copy one or more shapes as JSON.
  1176. * @param ids The ids of the shapes to copy.
  1177. * @param pageId The page from which to copy the shapes.
  1178. * @returns A string containing the JSON.
  1179. */
  1180. copyJson = (ids = this.selectedIds, pageId = this.currentPageId) => {
  1181. const shapes = ids.map((id) => this.getShape(id, pageId))
  1182. const json = JSON.stringify(shapes, null, 2)
  1183. TLDR.copyStringToClipboard(json)
  1184. return json
  1185. }
  1186. /* -------------------------------------------------- */
  1187. /* Camera */
  1188. /* -------------------------------------------------- */
  1189. /**
  1190. * Set the camera to a specific point and zoom.
  1191. * @param point The camera point (top left of the viewport).
  1192. * @param zoom The zoom level.
  1193. * @param reason Why did the camera change?
  1194. */
  1195. setCamera = (point: number[], zoom: number, reason: string): this => {
  1196. this.patchState(
  1197. {
  1198. document: {
  1199. pageStates: {
  1200. [this.currentPageId]: { camera: { point, zoom } },
  1201. },
  1202. },
  1203. },
  1204. reason
  1205. )
  1206. if (this.session) {
  1207. this.session.updateViewport(this.viewport)
  1208. }
  1209. return this
  1210. }
  1211. /**
  1212. * Reset the camera to the default position
  1213. */
  1214. resetCamera = (): this => {
  1215. return this.setCamera(this.centerPoint, 1, `reset_camera`)
  1216. }
  1217. /**
  1218. * Pan the camera
  1219. * @param delta
  1220. */
  1221. pan = (delta: number[]): this => {
  1222. const { camera } = this.pageState
  1223. return this.setCamera(Vec.round(Vec.sub(camera.point, delta)), camera.zoom, `panned`)
  1224. }
  1225. /**
  1226. * Pinch to a new zoom level, possibly together with a pan.
  1227. * @param point The current point under the cursor.
  1228. * @param delta The movement delta.
  1229. * @param zoomDelta The zoom detal
  1230. */
  1231. pinchZoom = (point: number[], delta: number[], zoom: number): this => {
  1232. const { camera } = this.pageState
  1233. const nextPoint = Vec.sub(camera.point, Vec.div(delta, camera.zoom))
  1234. const nextZoom = zoom
  1235. const p0 = Vec.sub(Vec.div(point, camera.zoom), nextPoint)
  1236. const p1 = Vec.sub(Vec.div(point, nextZoom), nextPoint)
  1237. return this.setCamera(Vec.round(Vec.add(nextPoint, Vec.sub(p1, p0))), nextZoom, `pinch_zoomed`)
  1238. }
  1239. /**
  1240. * Zoom to a new zoom level, keeping the point under the cursor in the same position
  1241. * @param next The new zoom level.
  1242. * @param center The point to zoom towards (defaults to screen center).
  1243. */
  1244. zoomTo = (next: number, center = this.centerPoint): this => {
  1245. const { zoom, point } = this.pageState.camera
  1246. const p0 = Vec.sub(Vec.div(center, zoom), point)
  1247. const p1 = Vec.sub(Vec.div(center, next), point)
  1248. return this.setCamera(Vec.round(Vec.add(point, Vec.sub(p1, p0))), next, `zoomed_camera`)
  1249. }
  1250. /**
  1251. * Zoom out by 25%
  1252. */
  1253. zoomIn = (): this => {
  1254. const i = Math.round((this.pageState.camera.zoom * 100) / 25)
  1255. const nextZoom = TLDR.getCameraZoom((i + 1) * 0.25)
  1256. return this.zoomTo(nextZoom)
  1257. }
  1258. /**
  1259. * Zoom in by 25%.
  1260. */
  1261. zoomOut = (): this => {
  1262. const i = Math.round((this.pageState.camera.zoom * 100) / 25)
  1263. const nextZoom = TLDR.getCameraZoom((i - 1) * 0.25)
  1264. return this.zoomTo(nextZoom)
  1265. }
  1266. /**
  1267. * Zoom to fit the page's shapes.
  1268. */
  1269. zoomToFit = (): this => {
  1270. const shapes = this.getShapes()
  1271. if (shapes.length === 0) return this
  1272. const bounds = Utils.getCommonBounds(shapes.map(TLDR.getBounds))
  1273. let zoom = TLDR.getCameraZoom(
  1274. Math.min(
  1275. (this.bounds.width - FIT_TO_SCREEN_PADDING) / bounds.width,
  1276. (this.bounds.height - FIT_TO_SCREEN_PADDING) / bounds.height
  1277. )
  1278. )
  1279. zoom =
  1280. this.pageState.camera.zoom === zoom || this.pageState.camera.zoom < 1
  1281. ? Math.min(1, zoom)
  1282. : zoom
  1283. const mx = (this.bounds.width - bounds.width * zoom) / 2 / zoom
  1284. const my = (this.bounds.height - bounds.height * zoom) / 2 / zoom
  1285. return this.setCamera(
  1286. Vec.round(Vec.add([-bounds.minX, -bounds.minY], [mx, my])),
  1287. zoom,
  1288. `zoomed_to_fit`
  1289. )
  1290. }
  1291. /**
  1292. * Zoom to the selected shapes.
  1293. */
  1294. zoomToSelection = (): this => {
  1295. if (this.selectedIds.length === 0) return this
  1296. const bounds = TLDR.getSelectedBounds(this.state)
  1297. let zoom = TLDR.getCameraZoom(
  1298. Math.min(
  1299. (this.bounds.width - FIT_TO_SCREEN_PADDING) / bounds.width,
  1300. (this.bounds.height - FIT_TO_SCREEN_PADDING) / bounds.height
  1301. )
  1302. )
  1303. zoom =
  1304. this.pageState.camera.zoom === zoom || this.pageState.camera.zoom < 1
  1305. ? Math.min(1, zoom)
  1306. : zoom
  1307. const mx = (this.bounds.width - bounds.width * zoom) / 2 / zoom
  1308. const my = (this.bounds.height - bounds.height * zoom) / 2 / zoom
  1309. return this.setCamera(
  1310. Vec.round(Vec.add([-bounds.minX, -bounds.minY], [mx, my])),
  1311. zoom,
  1312. `zoomed_to_selection`
  1313. )
  1314. }
  1315. /**
  1316. * Zoom back to content when the canvas is empty.
  1317. */
  1318. zoomToContent = (): this => {
  1319. const shapes = this.getShapes()
  1320. const pageState = this.pageState
  1321. if (shapes.length === 0) return this
  1322. const bounds = Utils.getCommonBounds(Object.values(shapes).map(TLDR.getBounds))
  1323. const { zoom } = pageState.camera
  1324. const mx = (this.bounds.width - bounds.width * zoom) / 2 / zoom
  1325. const my = (this.bounds.height - bounds.height * zoom) / 2 / zoom
  1326. return this.setCamera(
  1327. Vec.round(Vec.add([-bounds.minX, -bounds.minY], [mx, my])),
  1328. this.pageState.camera.zoom,
  1329. `zoomed_to_content`
  1330. )
  1331. }
  1332. /**
  1333. * Zoom the camera to 100%.
  1334. */
  1335. zoomToActual = (): this => {
  1336. return this.zoomTo(1)
  1337. }
  1338. /**
  1339. * Zoom the camera by a certain delta.
  1340. * @param delta The zoom delta.
  1341. * @param center The point to zoom toward.
  1342. */
  1343. zoom = Utils.throttle((delta: number, center?: number[]): this => {
  1344. const { zoom } = this.pageState.camera
  1345. const nextZoom = TLDR.getCameraZoom(zoom - delta * zoom)
  1346. return this.zoomTo(nextZoom, center)
  1347. }, 16)
  1348. /* -------------------------------------------------- */
  1349. /* Selection */
  1350. /* -------------------------------------------------- */
  1351. /**
  1352. * Clear the selection history (undo/redo stack for selection).
  1353. */
  1354. private clearSelectHistory = (): this => {
  1355. this.selectHistory.pointer = 0
  1356. this.selectHistory.stack = [this.selectedIds]
  1357. return this
  1358. }
  1359. /**
  1360. * Adds a selection to the selection history (undo/redo stack for selection).
  1361. */
  1362. private addToSelectHistory = (ids: string[]): this => {
  1363. if (this.selectHistory.pointer < this.selectHistory.stack.length) {
  1364. this.selectHistory.stack = this.selectHistory.stack.slice(0, this.selectHistory.pointer + 1)
  1365. }
  1366. this.selectHistory.pointer++
  1367. this.selectHistory.stack.push(ids)
  1368. return this
  1369. }
  1370. /**
  1371. * Set the current selection.
  1372. * @param ids The ids to select
  1373. * @param push Whether to add the ids to the current selection instead.
  1374. */
  1375. private setSelectedIds = (ids: string[], push = false): this => {
  1376. const nextIds = push ? [...this.pageState.selectedIds, ...ids] : [...ids]
  1377. if (this.state.room) {
  1378. const { users, userId } = this.state.room
  1379. this._onUserChange?.(this, {
  1380. ...users[userId],
  1381. selectedIds: nextIds,
  1382. })
  1383. }
  1384. return this.patchState(
  1385. {
  1386. appState: {
  1387. activeTool: 'select',
  1388. },
  1389. document: {
  1390. pageStates: {
  1391. [this.currentPageId]: {
  1392. selectedIds: nextIds,
  1393. },
  1394. },
  1395. },
  1396. },
  1397. `selected`
  1398. )
  1399. }
  1400. /**
  1401. * Undo the most recent selection.
  1402. */
  1403. undoSelect = (): this => {
  1404. if (this.selectHistory.pointer > 0) {
  1405. this.selectHistory.pointer--
  1406. this.setSelectedIds(this.selectHistory.stack[this.selectHistory.pointer])
  1407. }
  1408. return this
  1409. }
  1410. /**
  1411. * Redo the previous selection.
  1412. */
  1413. redoSelect = (): this => {
  1414. if (this.selectHistory.pointer < this.selectHistory.stack.length - 1) {
  1415. this.selectHistory.pointer++
  1416. this.setSelectedIds(this.selectHistory.stack[this.selectHistory.pointer])
  1417. }
  1418. return this
  1419. }
  1420. /**
  1421. * Select one or more shapes.
  1422. * @param ids The shape ids to select.
  1423. */
  1424. select = (...ids: string[]): this => {
  1425. ids.forEach((id) => {
  1426. if (!this.page.shapes[id]) {
  1427. throw Error(`That shape does not exist on page ${this.currentPageId}`)
  1428. }
  1429. })
  1430. this.setSelectedIds(ids)
  1431. this.addToSelectHistory(ids)
  1432. return this
  1433. }
  1434. /**
  1435. * Select all shapes on the page.
  1436. */
  1437. selectAll = (pageId = this.currentPageId): this => {
  1438. if (this.session) return this
  1439. // Select only shapes that are the direct child of the page
  1440. this.setSelectedIds(
  1441. Object.values(this.document.pages[pageId].shapes)
  1442. .filter((shape) => shape.parentId === pageId)
  1443. .map((shape) => shape.id)
  1444. )
  1445. this.addToSelectHistory(this.selectedIds)
  1446. if (this.appState.activeTool !== 'select') {
  1447. this.selectTool('select')
  1448. }
  1449. return this
  1450. }
  1451. /**
  1452. * Deselect any selected shapes.
  1453. */
  1454. deselectAll = (): this => {
  1455. this.setSelectedIds([])
  1456. this.addToSelectHistory(this.selectedIds)
  1457. return this
  1458. }
  1459. /* -------------------------------------------------- */
  1460. /* Sessions p */
  1461. /* -------------------------------------------------- */
  1462. /**
  1463. * Start a new session.
  1464. * @param session The new session
  1465. * @param args arguments of the session's start method.
  1466. */
  1467. startSession = <T extends SessionType>(type: T, ...args: ExceptFirstTwo<ArgsOfType<T>>): this => {
  1468. if (this.readOnly && type !== SessionType.Brush) return this
  1469. if (this.session) {
  1470. throw Error(`Already in a session! (${this.session.constructor.name})`)
  1471. }
  1472. const Session = getSession(type)
  1473. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  1474. // @ts-ignore
  1475. this.session = new Session(this.state, this.viewport, ...args)
  1476. const result = this.session.start(this.state)
  1477. if (result) {
  1478. this.patchState(
  1479. {
  1480. ...result,
  1481. appState: {
  1482. ...result.appState,
  1483. },
  1484. },
  1485. `session:start_${this.session.constructor.name}`
  1486. )
  1487. }
  1488. return this
  1489. // return this.setStatus(this.session.status)
  1490. }
  1491. /**
  1492. * updateSession.
  1493. * @param args The arguments of the current session's update method.
  1494. */
  1495. updateSession = <T extends Session>(...args: ExceptFirst<Parameters<T['update']>>): this => {
  1496. const { session } = this
  1497. if (!session) return this
  1498. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  1499. // @ts-ignore
  1500. const patch = session.update(this.state, ...args)
  1501. if (!patch) return this
  1502. return this.patchState(patch, `session:${session?.constructor.name}`)
  1503. }
  1504. /**
  1505. * Cancel the current session.
  1506. * @param args The arguments of the current session's cancel method.
  1507. */
  1508. cancelSession = (): this => {
  1509. const { session } = this
  1510. if (!session) return this
  1511. this.session = undefined
  1512. const result = session.cancel(this.state)
  1513. if (result) {
  1514. this.patchState(result, `session:cancel:${session.constructor.name}`)
  1515. }
  1516. return this
  1517. }
  1518. /**
  1519. * Complete the current session.
  1520. * @param args The arguments of the current session's complete method.
  1521. */
  1522. completeSession = (): this => {
  1523. const { session } = this
  1524. if (!session) return this
  1525. this.session = undefined
  1526. const result = session.complete(this.state)
  1527. if (result === undefined) {
  1528. this.isCreating = false
  1529. return this.patchState(
  1530. {
  1531. appState: {
  1532. status: TLDrawStatus.Idle,
  1533. },
  1534. document: {
  1535. pageStates: {
  1536. [this.currentPageId]: {
  1537. editingId: undefined,
  1538. bindingId: undefined,
  1539. hoveredId: undefined,
  1540. },
  1541. },
  1542. },
  1543. },
  1544. `session:complete:${session.constructor.name}`
  1545. )
  1546. } else if ('after' in result) {
  1547. // Session ended with a command
  1548. if (this.isCreating) {
  1549. // We're currently creating a shape. Override the command's
  1550. // before state so that when we undo the command, we remove
  1551. // the shape we just created.
  1552. result.before = {
  1553. appState: {
  1554. ...result.before.appState,
  1555. status: TLDrawStatus.Idle,
  1556. },
  1557. document: {
  1558. pages: {
  1559. [this.currentPageId]: {
  1560. shapes: Object.fromEntries(this.selectedIds.map((id) => [id, undefined])),
  1561. },
  1562. },
  1563. pageStates: {
  1564. [this.currentPageId]: {
  1565. selectedIds: [],
  1566. editingId: null,
  1567. bindingId: null,
  1568. hoveredId: null,
  1569. },
  1570. },
  1571. },
  1572. }
  1573. if (this.appState.isToolLocked) {
  1574. const pageState = result.after?.document?.pageStates?.[this.currentPageId] || {}
  1575. pageState.selectedIds = []
  1576. }
  1577. this.isCreating = false
  1578. }
  1579. result.after.appState = {
  1580. ...result.after.appState,
  1581. status: TLDrawStatus.Idle,
  1582. }
  1583. result.after.document = {
  1584. ...result.after.document,
  1585. pageStates: {
  1586. ...result.after.document?.pageStates,
  1587. [this.currentPageId]: {
  1588. ...(result.after.document?.pageStates || {})[this.currentPageId],
  1589. editingId: null,
  1590. },
  1591. },
  1592. }
  1593. this.setState(result, `session:complete:${session.constructor.name}`)
  1594. } else {
  1595. this.patchState(
  1596. {
  1597. ...result,
  1598. appState: {
  1599. ...result.appState,
  1600. status: TLDrawStatus.Idle,
  1601. },
  1602. document: {
  1603. pageStates: {
  1604. [this.currentPageId]: {
  1605. ...result.document?.pageStates?.[this.currentPageId],
  1606. editingId: null,
  1607. },
  1608. },
  1609. },
  1610. },
  1611. `session:complete:${session.constructor.name}`
  1612. )
  1613. }
  1614. const { isToolLocked, activeTool } = this.appState
  1615. if (!isToolLocked && activeTool !== TLDrawShapeType.Draw) {
  1616. this.selectTool('select')
  1617. }
  1618. return this
  1619. }
  1620. /* -------------------------------------------------- */
  1621. /* Shape Functions */
  1622. /* -------------------------------------------------- */
  1623. /**
  1624. * Manually create shapes on the page.
  1625. * @param shapes An array of shape partials, containing the initial props for the shapes.
  1626. * @command
  1627. */
  1628. createShapes = (
  1629. ...shapes: ({ id: string; type: TLDrawShapeType } & Partial<TLDrawShape>)[]
  1630. ): this => {
  1631. if (shapes.length === 0) return this
  1632. return this.create(
  1633. shapes.map((shape) => {
  1634. return TLDR.getShapeUtils(shape.type).create({
  1635. parentId: this.currentPageId,
  1636. ...shape,
  1637. })
  1638. })
  1639. )
  1640. }
  1641. /**
  1642. * Manually update a set of shapes.
  1643. * @param shapes An array of shape partials, containing the changes to be made to each shape.
  1644. * @command
  1645. */
  1646. updateShapes = (...shapes: ({ id: string } & Partial<TLDrawShape>)[]): this => {
  1647. const pageShapes = this.document.pages[this.currentPageId].shapes
  1648. const shapesToUpdate = shapes.filter((shape) => pageShapes[shape.id])
  1649. if (shapesToUpdate.length === 0) return this
  1650. return this.setState(
  1651. Commands.update(this.state, shapesToUpdate, this.currentPageId),
  1652. 'updated_shapes'
  1653. )
  1654. }
  1655. /**
  1656. * Manually patch a set of shapes.
  1657. * @param shapes An array of shape partials, containing the changes to be made to each shape.
  1658. * @command
  1659. */
  1660. patchShapes = (...shapes: ({ id: string } & Partial<TLDrawShape>)[]): this => {
  1661. const pageShapes = this.document.pages[this.currentPageId].shapes
  1662. const shapesToUpdate = shapes.filter((shape) => pageShapes[shape.id])
  1663. if (shapesToUpdate.length === 0) return this
  1664. return this.patchState(
  1665. Commands.update(this.state, shapesToUpdate, this.currentPageId).after,
  1666. 'updated_shapes'
  1667. )
  1668. }
  1669. createTextShapeAtPoint(point: number[]): this {
  1670. const {
  1671. shapes,
  1672. appState: { currentPageId, currentStyle },
  1673. } = this
  1674. const childIndex =
  1675. shapes.length === 0
  1676. ? 1
  1677. : shapes
  1678. .filter((shape) => shape.parentId === currentPageId)
  1679. .sort((a, b) => b.childIndex - a.childIndex)[0].childIndex + 1
  1680. const id = Utils.uniqueId()
  1681. const Text = shapeUtils[TLDrawShapeType.Text]
  1682. const newShape = Text.create({
  1683. id,
  1684. parentId: currentPageId,
  1685. childIndex,
  1686. point,
  1687. style: { ...currentStyle },
  1688. })
  1689. const bounds = Text.getBounds(newShape)
  1690. newShape.point = Vec.sub(newShape.point, [bounds.width / 2, bounds.height / 2])
  1691. this.createShapes(newShape)
  1692. this.setEditingId(id)
  1693. return this
  1694. }
  1695. /**
  1696. * Create one or more shapes.
  1697. * @param shapes An array of shapes.
  1698. * @command
  1699. */
  1700. create = (shapes: TLDrawShape[] = [], bindings: TLDrawBinding[] = []): this => {
  1701. if (shapes.length === 0) return this
  1702. return this.setState(Commands.createShapes(this.state, shapes, bindings))
  1703. }
  1704. /**
  1705. * Patch in a new set of shapes
  1706. * @param shapes
  1707. * @param bindings
  1708. */
  1709. patchCreate = (shapes: TLDrawShape[] = [], bindings: TLDrawBinding[] = []): this => {
  1710. if (shapes.length === 0) return this
  1711. return this.patchState(Commands.createShapes(this.state, shapes, bindings).after)
  1712. }
  1713. /**
  1714. * Delete one or more shapes.
  1715. * @param ids The ids of the shapes to delete.
  1716. * @command
  1717. */
  1718. delete = (ids = this.selectedIds): this => {
  1719. if (ids.length === 0) return this
  1720. return this.setState(Commands.deleteShapes(this.state, ids))
  1721. }
  1722. /**
  1723. * Delete all shapes on the page.
  1724. */
  1725. clear = (): this => {
  1726. this.selectAll()
  1727. this.delete()
  1728. return this
  1729. }
  1730. /**
  1731. * Change the style for one or more shapes.
  1732. * @param style A style partial to apply to the shapes.
  1733. * @param ids The ids of the shapes to change (defaults to selection).
  1734. */
  1735. style = (style: Partial<ShapeStyles>, ids = this.selectedIds): this => {
  1736. return this.setState(Commands.styleShapes(this.state, ids, style))
  1737. }
  1738. /**
  1739. * Align one or more shapes.
  1740. * @param direction Whether to align horizontally or vertically.
  1741. * @param ids The ids of the shapes to change (defaults to selection).
  1742. */
  1743. align = (type: AlignType, ids = this.selectedIds): this => {
  1744. if (ids.length < 2) return this
  1745. return this.setState(Commands.alignShapes(this.state, ids, type))
  1746. }
  1747. /**
  1748. * Distribute one or more shapes.
  1749. * @param direction Whether to distribute horizontally or vertically..
  1750. * @param ids The ids of the shapes to change (defaults to selection).
  1751. */
  1752. distribute = (direction: DistributeType, ids = this.selectedIds): this => {
  1753. if (ids.length < 3) return this
  1754. return this.setState(Commands.distributeShapes(this.state, ids, direction))
  1755. }
  1756. /**
  1757. * Stretch one or more shapes to their common bounds.
  1758. * @param direction Whether to stretch horizontally or vertically.
  1759. * @param ids The ids of the shapes to change (defaults to selection).
  1760. */
  1761. stretch = (direction: StretchType, ids = this.selectedIds): this => {
  1762. if (ids.length < 2) return this
  1763. return this.setState(Commands.stretchShapes(this.state, ids, direction))
  1764. }
  1765. /**
  1766. * Flip one or more shapes horizontally.
  1767. * @param ids The ids of the shapes to change (defaults to selection).
  1768. */
  1769. flipHorizontal = (ids = this.selectedIds): this => {
  1770. if (ids.length === 0) return this
  1771. return this.setState(Commands.flipShapes(this.state, ids, FlipType.Horizontal))
  1772. }
  1773. /**
  1774. * Flip one or more shapes vertically.
  1775. * @param ids The ids of the shapes to change (defaults to selection).
  1776. */
  1777. flipVertical = (ids = this.selectedIds): this => {
  1778. if (ids.length === 0) return this
  1779. return this.setState(Commands.flipShapes(this.state, ids, FlipType.Vertical))
  1780. }
  1781. /**
  1782. * Move one or more shapes to a new page. Will also break or move bindings.
  1783. * @param toPageId The id of the page to move the shapes to.
  1784. * @param fromPageId The id of the page to move the shapes from (defaults to current page).
  1785. * @param ids The ids of the shapes to move (defaults to selection).
  1786. */
  1787. moveToPage = (
  1788. toPageId: string,
  1789. fromPageId = this.currentPageId,
  1790. ids = this.selectedIds
  1791. ): this => {
  1792. if (ids.length === 0) return this
  1793. this.setState(Commands.moveShapesToPage(this.state, ids, this.bounds, fromPageId, toPageId))
  1794. return this
  1795. }
  1796. /**
  1797. * Move one or more shapes to the back of the page.
  1798. * @param ids The ids of the shapes to change (defaults to selection).
  1799. */
  1800. moveToBack = (ids = this.selectedIds): this => {
  1801. if (ids.length === 0) return this
  1802. return this.setState(Commands.reorderShapes(this.state, ids, MoveType.ToBack))
  1803. }
  1804. /**
  1805. * Move one or more shapes backward on of the page.
  1806. * @param ids The ids of the shapes to change (defaults to selection).
  1807. */
  1808. moveBackward = (ids = this.selectedIds): this => {
  1809. if (ids.length === 0) return this
  1810. return this.setState(Commands.reorderShapes(this.state, ids, MoveType.Backward))
  1811. }
  1812. /**
  1813. * Move one or more shapes forward on the page.
  1814. * @param ids The ids of the shapes to change (defaults to selection).
  1815. */
  1816. moveForward = (ids = this.selectedIds): this => {
  1817. if (ids.length === 0) return this
  1818. return this.setState(Commands.reorderShapes(this.state, ids, MoveType.Forward))
  1819. }
  1820. /**
  1821. * Move one or more shapes to the front of the page.
  1822. * @param ids The ids of the shapes to change (defaults to selection).
  1823. */
  1824. moveToFront = (ids = this.selectedIds): this => {
  1825. if (ids.length === 0) return this
  1826. return this.setState(Commands.reorderShapes(this.state, ids, MoveType.ToFront))
  1827. }
  1828. /**
  1829. * Nudge one or more shapes in a direction.
  1830. * @param delta The direction to nudge the shapes.
  1831. * @param isMajor Whether this is a major (i.e. shift) nudge.
  1832. * @param ids The ids to change (defaults to selection).
  1833. */
  1834. nudge = (delta: number[], isMajor = false, ids = this.selectedIds): this => {
  1835. if (ids.length === 0) return this
  1836. return this.setState(
  1837. Commands.translateShapes(this.state, ids, Vec.mul(delta, isMajor ? 10 : 1))
  1838. )
  1839. }
  1840. /**
  1841. * Duplicate one or more shapes.
  1842. * @param ids The ids to duplicate (defaults to selection).
  1843. */
  1844. duplicate = (ids = this.selectedIds, point?: number[]): this => {
  1845. if (this.readOnly) return this
  1846. if (ids.length === 0) return this
  1847. return this.setState(Commands.duplicateShapes(this.state, ids, point))
  1848. }
  1849. /**
  1850. * Reset the bounds for one or more shapes. Usually when the
  1851. * bounding box of a shape is double-clicked. Different shapes may
  1852. * handle this differently.
  1853. * @param ids The ids to change (defaults to selection).
  1854. */
  1855. resetBounds = (ids = this.selectedIds): this => {
  1856. const command = Commands.resetBounds(this.state, ids, this.currentPageId)
  1857. return this.setState(Commands.resetBounds(this.state, ids, this.currentPageId), command.id)
  1858. }
  1859. /**
  1860. * Toggle the hidden property of one or more shapes.
  1861. * @param ids The ids to change (defaults to selection).
  1862. */
  1863. toggleHidden = (ids = this.selectedIds): this => {
  1864. if (ids.length === 0) return this
  1865. return this.setState(Commands.toggleShapeProp(this.state, ids, 'isHidden'))
  1866. }
  1867. /**
  1868. * Toggle the locked property of one or more shapes.
  1869. * @param ids The ids to change (defaults to selection).
  1870. */
  1871. toggleLocked = (ids = this.selectedIds): this => {
  1872. if (ids.length === 0) return this
  1873. return this.setState(Commands.toggleShapeProp(this.state, ids, 'isLocked'))
  1874. }
  1875. /**
  1876. * Toggle the fixed-aspect-ratio property of one or more shapes.
  1877. * @param ids The ids to change (defaults to selection).
  1878. */
  1879. toggleAspectRatioLocked = (ids = this.selectedIds): this => {
  1880. if (ids.length === 0) return this
  1881. return this.setState(Commands.toggleShapeProp(this.state, ids, 'isAspectRatioLocked'))
  1882. }
  1883. /**
  1884. * Toggle the decoration at a handle of one or more shapes.
  1885. * @param handleId The handle to toggle.
  1886. * @param ids The ids of the shapes to toggle the decoration on.
  1887. */
  1888. toggleDecoration = (handleId: string, ids = this.selectedIds): this => {
  1889. if (ids.length === 0 || !(handleId === 'start' || handleId === 'end')) return this
  1890. return this.setState(Commands.toggleShapesDecoration(this.state, ids, handleId))
  1891. }
  1892. /**
  1893. * Rotate one or more shapes by a delta.
  1894. * @param delta The delta in radians.
  1895. * @param ids The ids to rotate (defaults to selection).
  1896. */
  1897. rotate = (delta = Math.PI * -0.5, ids = this.selectedIds): this => {
  1898. if (ids.length === 0) return this
  1899. const change = Commands.rotateShapes(this.state, ids, delta)
  1900. if (!change) return this
  1901. return this.setState(change)
  1902. }
  1903. /**
  1904. * Group the selected shapes.
  1905. * @param ids The ids to group (defaults to selection).
  1906. * @param groupId The new group's id.
  1907. */
  1908. group = (
  1909. ids = this.selectedIds,
  1910. groupId = Utils.uniqueId(),
  1911. pageId = this.currentPageId
  1912. ): this => {
  1913. if (this.readOnly) return this
  1914. if (ids.length === 1 && this.getShape(ids[0], pageId).type === TLDrawShapeType.Group) {
  1915. return this.ungroup(ids, pageId)
  1916. }
  1917. if (ids.length < 2) return this
  1918. const command = Commands.groupShapes(this.state, ids, groupId, pageId)
  1919. if (!command) return this
  1920. return this.setState(command)
  1921. }
  1922. /**
  1923. * Ungroup the selected groups.
  1924. * @todo
  1925. */
  1926. ungroup = (ids = this.selectedIds, pageId = this.currentPageId): this => {
  1927. if (this.readOnly) return this
  1928. const groups = ids
  1929. .map((id) => this.getShape(id, pageId))
  1930. .filter((shape) => shape.type === TLDrawShapeType.Group)
  1931. if (groups.length === 0) return this
  1932. const command = Commands.ungroupShapes(this.state, ids, groups as GroupShape[], pageId)
  1933. if (!command) return this
  1934. return this.setState(command)
  1935. }
  1936. /**
  1937. * Cancel the current session.
  1938. */
  1939. cancel = (): this => {
  1940. this.currentTool.onCancel?.()
  1941. return this
  1942. }
  1943. /* -------------------------------------------------- */
  1944. /* Event Handlers */
  1945. /* -------------------------------------------------- */
  1946. /* ----------------- Keyboard Events ---------------- */
  1947. onKeyDown: TLKeyboardEventHandler = (key, info, e) => {
  1948. if (key === 'Escape') {
  1949. this.cancel()
  1950. return
  1951. }
  1952. this.currentTool.onKeyDown?.(key, info, e)
  1953. return this
  1954. }
  1955. onKeyUp: TLKeyboardEventHandler = (key, info, e) => {
  1956. if (!info) return
  1957. this.currentTool.onKeyUp?.(key, info, e)
  1958. }
  1959. /* ------------- Renderer Event Handlers ------------ */
  1960. onPinchStart: TLPinchEventHandler = (info, e) => this.currentTool.onPinchStart?.(info, e)
  1961. onPinchEnd: TLPinchEventHandler = (info, e) => this.currentTool.onPinchEnd?.(info, e)
  1962. onPinch: TLPinchEventHandler = (info, e) => this.currentTool.onPinch?.(info, e)
  1963. onPan: TLWheelEventHandler = (info, e) => {
  1964. if (this.appState.status === 'pinching') return
  1965. // TODO: Pan and pinchzoom are firing at the same time. Considering turning one of them off!
  1966. const delta = Vec.div(info.delta, this.pageState.camera.zoom)
  1967. const prev = this.pageState.camera.point
  1968. const next = Vec.sub(prev, delta)
  1969. if (Vec.isEqual(next, prev)) return
  1970. this.pan(delta)
  1971. // onPan is called by onPointerMove when spaceKey is pressed,
  1972. // so we shouldn't call this again.
  1973. if (!info.spaceKey) {
  1974. this.onPointerMove(info, e as unknown as React.PointerEvent)
  1975. }
  1976. }
  1977. onZoom: TLWheelEventHandler = (info, e) => {
  1978. if (this.state.appState.status !== TLDrawStatus.Idle) return
  1979. this.zoom(info.delta[2] / 100, info.delta)
  1980. this.onPointerMove(info, e as unknown as React.PointerEvent)
  1981. }
  1982. /* ----------------- Pointer Events ----------------- */
  1983. onPointerMove: TLPointerEventHandler = (info, e) => {
  1984. // Several events (e.g. pan) can trigger the same "pointer move" behavior
  1985. this.currentTool.onPointerMove?.(info, e)
  1986. this.pointerPoint = this.getPagePoint(info.point)
  1987. // Move this to an emitted event
  1988. if (this.state.room) {
  1989. const { users, userId } = this.state.room
  1990. this._onUserChange?.(this, {
  1991. ...users[userId],
  1992. point: this.getPagePoint(info.point),
  1993. })
  1994. }
  1995. }
  1996. onPointerDown: TLPointerEventHandler = (...args) => this.currentTool.onPointerDown?.(...args)
  1997. onPointerUp: TLPointerEventHandler = (...args) => this.currentTool.onPointerUp?.(...args)
  1998. // Canvas (background)
  1999. onPointCanvas: TLCanvasEventHandler = (...args) => this.currentTool.onPointCanvas?.(...args)
  2000. onDoubleClickCanvas: TLCanvasEventHandler = (...args) =>
  2001. this.currentTool.onDoubleClickCanvas?.(...args)
  2002. onRightPointCanvas: TLCanvasEventHandler = (...args) =>
  2003. this.currentTool.onRightPointCanvas?.(...args)
  2004. onDragCanvas: TLCanvasEventHandler = (...args) => this.currentTool.onDragCanvas?.(...args)
  2005. onReleaseCanvas: TLCanvasEventHandler = (...args) => this.currentTool.onReleaseCanvas?.(...args)
  2006. // Shape
  2007. onPointShape: TLPointerEventHandler = (...args) => this.currentTool.onPointShape?.(...args)
  2008. onReleaseShape: TLPointerEventHandler = (...args) => this.currentTool.onReleaseShape?.(...args)
  2009. onDoubleClickShape: TLPointerEventHandler = (...args) =>
  2010. this.currentTool.onDoubleClickShape?.(...args)
  2011. onRightPointShape: TLPointerEventHandler = (...args) =>
  2012. this.currentTool.onRightPointShape?.(...args)
  2013. onDragShape: TLPointerEventHandler = (...args) => this.currentTool.onDragShape?.(...args)
  2014. onHoverShape: TLPointerEventHandler = (...args) => this.currentTool.onHoverShape?.(...args)
  2015. onUnhoverShape: TLPointerEventHandler = (...args) => this.currentTool.onUnhoverShape?.(...args)
  2016. // Bounds (bounding box background)
  2017. onPointBounds: TLBoundsEventHandler = (...args) => this.currentTool.onPointBounds?.(...args)
  2018. onDoubleClickBounds: TLBoundsEventHandler = (...args) =>
  2019. this.currentTool.onDoubleClickBounds?.(...args)
  2020. onRightPointBounds: TLBoundsEventHandler = (...args) =>
  2021. this.currentTool.onRightPointBounds?.(...args)
  2022. onDragBounds: TLBoundsEventHandler = (...args) => this.currentTool.onDragBounds?.(...args)
  2023. onHoverBounds: TLBoundsEventHandler = (...args) => this.currentTool.onHoverBounds?.(...args)
  2024. onUnhoverBounds: TLBoundsEventHandler = (...args) => this.currentTool.onUnhoverBounds?.(...args)
  2025. onReleaseBounds: TLBoundsEventHandler = (...args) => this.currentTool.onReleaseBounds?.(...args)
  2026. // Bounds handles (corners, edges)
  2027. onPointBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2028. this.currentTool.onPointBoundsHandle?.(...args)
  2029. onDoubleClickBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2030. this.currentTool.onDoubleClickBoundsHandle?.(...args)
  2031. onRightPointBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2032. this.currentTool.onRightPointBoundsHandle?.(...args)
  2033. onDragBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2034. this.currentTool.onDragBoundsHandle?.(...args)
  2035. onHoverBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2036. this.currentTool.onHoverBoundsHandle?.(...args)
  2037. onUnhoverBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2038. this.currentTool.onUnhoverBoundsHandle?.(...args)
  2039. onReleaseBoundsHandle: TLBoundsHandleEventHandler = (...args) =>
  2040. this.currentTool.onReleaseBoundsHandle?.(...args)
  2041. // Handles (ie the handles of a selected arrow)
  2042. onPointHandle: TLPointerEventHandler = (...args) => this.currentTool.onPointHandle?.(...args)
  2043. onDoubleClickHandle: TLPointerEventHandler = (...args) =>
  2044. this.currentTool.onDoubleClickHandle?.(...args)
  2045. onRightPointHandle: TLPointerEventHandler = (...args) =>
  2046. this.currentTool.onRightPointHandle?.(...args)
  2047. onDragHandle: TLPointerEventHandler = (...args) => this.currentTool.onDragHandle?.(...args)
  2048. onHoverHandle: TLPointerEventHandler = (...args) => this.currentTool.onHoverHandle?.(...args)
  2049. onUnhoverHandle: TLPointerEventHandler = (...args) => this.currentTool.onUnhoverHandle?.(...args)
  2050. onReleaseHandle: TLPointerEventHandler = (...args) => this.currentTool.onReleaseHandle?.(...args)
  2051. onShapeChange = (shape: { id: string } & Partial<TLDrawShape>) => {
  2052. this.updateShapes(shape)
  2053. }
  2054. onShapeBlur = () => {
  2055. const { editingId } = this.pageState
  2056. if (editingId) {
  2057. // If we're editing text, then delete the text if it's empty
  2058. const shape = this.getShape(editingId)
  2059. this.setEditingId()
  2060. if (shape.type === TLDrawShapeType.Text) {
  2061. if (shape.text.trim().length <= 0) {
  2062. this.setState(Commands.deleteShapes(this.state, [editingId]), 'delete_empty_text')
  2063. } else {
  2064. this.select(editingId)
  2065. }
  2066. }
  2067. }
  2068. this.currentTool.onShapeBlur?.()
  2069. }
  2070. onShapeClone: TLShapeCloneHandler = (info, e) => this.currentTool.onShapeClone?.(info, e)
  2071. onRenderCountChange = (ids: string[]) => {
  2072. const appState = this.getAppState()
  2073. if (appState.isEmptyCanvas && ids.length > 0) {
  2074. this.patchState(
  2075. {
  2076. appState: {
  2077. isEmptyCanvas: false,
  2078. },
  2079. },
  2080. 'empty_canvas:false'
  2081. )
  2082. } else if (!appState.isEmptyCanvas && ids.length <= 0) {
  2083. this.patchState(
  2084. {
  2085. appState: {
  2086. isEmptyCanvas: true,
  2087. },
  2088. },
  2089. 'empty_canvas:true'
  2090. )
  2091. }
  2092. }
  2093. onError = () => {
  2094. // TODO
  2095. }
  2096. isSelected(id: string) {
  2097. return this.selectedIds.includes(id)
  2098. }
  2099. get isLocal() {
  2100. return this.state.room === undefined || this.state.room.id === 'local'
  2101. }
  2102. get status() {
  2103. return this.appState.status
  2104. }
  2105. get currentUser() {
  2106. if (!this.state.room) return
  2107. return this.state.room.users[this.state.room.userId]
  2108. }
  2109. get centerPoint() {
  2110. return Vec.round([this.bounds.width / 2, this.bounds.height / 2])
  2111. }
  2112. get viewport() {
  2113. const { camera } = this.pageState
  2114. const { width, height } = this.bounds
  2115. const [minX, minY] = Vec.sub(Vec.div([0, 0], camera.zoom), camera.point)
  2116. const [maxX, maxY] = Vec.sub(Vec.div([width, height], camera.zoom), camera.point)
  2117. return {
  2118. minX,
  2119. minY,
  2120. maxX,
  2121. maxY,
  2122. height: maxX - minX,
  2123. width: maxY - minY,
  2124. }
  2125. }
  2126. static version = 13
  2127. static defaultDocument: TLDrawDocument = {
  2128. id: 'doc',
  2129. name: 'New Document',
  2130. version: 13,
  2131. pages: {
  2132. page: {
  2133. id: 'page',
  2134. name: 'Page 1',
  2135. childIndex: 1,
  2136. shapes: {},
  2137. bindings: {},
  2138. },
  2139. },
  2140. pageStates: {
  2141. page: {
  2142. id: 'page',
  2143. selectedIds: [],
  2144. camera: {
  2145. point: [0, 0],
  2146. zoom: 1,
  2147. },
  2148. },
  2149. },
  2150. }
  2151. static defaultState: Data = {
  2152. settings: {
  2153. isPenMode: false,
  2154. isDarkMode: false,
  2155. isZoomSnap: false,
  2156. isFocusMode: false,
  2157. isSnapping: false,
  2158. isDebugMode: process.env.NODE_ENV === 'development',
  2159. isReadonlyMode: false,
  2160. nudgeDistanceLarge: 16,
  2161. nudgeDistanceSmall: 1,
  2162. showRotateHandles: true,
  2163. showBindingHandles: true,
  2164. showCloneHandles: false,
  2165. },
  2166. appState: {
  2167. activeTool: 'select',
  2168. hoveredId: undefined,
  2169. currentPageId: 'page',
  2170. pages: [{ id: 'page', name: 'page', childIndex: 1 }],
  2171. currentStyle: defaultStyle,
  2172. selectedStyle: defaultStyle,
  2173. isToolLocked: false,
  2174. isStyleOpen: false,
  2175. isEmptyCanvas: false,
  2176. status: TLDrawStatus.Idle,
  2177. snapLines: [],
  2178. },
  2179. document: TLDrawState.defaultDocument,
  2180. room: {
  2181. id: 'local',
  2182. userId: uuid,
  2183. users: {
  2184. [uuid]: {
  2185. id: uuid,
  2186. color: USER_COLORS[0],
  2187. point: [100, 100],
  2188. selectedIds: [],
  2189. activeShapes: [],
  2190. },
  2191. },
  2192. },
  2193. }
  2194. }