소스 검색

Remove core example

main
Steve Ruiz 3 년 전
부모
커밋
742f9421d6
4개의 변경된 파일0개의 추가작업 그리고 450개의 파일을 삭제
  1. 0
    138
      packages/dev/src/core/box.tsx
  2. 0
    35
      packages/dev/src/core/index.tsx
  3. 0
    124
      packages/dev/src/core/label.tsx
  4. 0
    153
      packages/dev/src/core/state.ts

+ 0
- 138
packages/dev/src/core/box.tsx 파일 보기

@@ -1,138 +0,0 @@
1
-/* eslint-disable @typescript-eslint/no-non-null-assertion */
2
-/* refresh-reset */
3
-
4
-import * as React from 'react'
5
-import { TLShape, Utils, TLBounds, TLShapeUtil, HTMLContainer, SVGContainer } from '@tldraw/core'
6
-
7
-// Define a custom shape
8
-
9
-export interface BoxShape extends TLShape {
10
-  type: 'box'
11
-  size: number[]
12
-  text: string
13
-}
14
-
15
-export const boxShape: BoxShape = {
16
-  id: 'example1',
17
-  type: 'box',
18
-  parentId: 'page',
19
-  childIndex: 0,
20
-  name: 'Example Shape',
21
-  point: [0, 0],
22
-  size: [100, 100],
23
-  rotation: 0,
24
-  text: 'Hello world!',
25
-}
26
-// Create a "shape utility" class that interprets that shape
27
-
28
-export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> {
29
-  age = 100
30
-
31
-  Component = TLShapeUtil.Component<BoxShape, HTMLDivElement>(
32
-    ({ shape, events, onShapeChange, isEditing, meta }, ref) => {
33
-      const color = meta.isDarkMode ? 'white' : 'black'
34
-
35
-      const rInput = React.useRef<HTMLDivElement>(null)
36
-
37
-      function updateShapeSize() {
38
-        const elm = rInput.current!
39
-
40
-        onShapeChange?.({
41
-          ...shape,
42
-          text: elm.innerText,
43
-          size: [elm.offsetWidth + 44, elm.offsetHeight + 44],
44
-        })
45
-      }
46
-
47
-      React.useLayoutEffect(() => {
48
-        const elm = rInput.current!
49
-
50
-        const observer = new MutationObserver(updateShapeSize)
51
-
52
-        observer.observe(elm, {
53
-          attributes: true,
54
-          characterData: true,
55
-          subtree: true,
56
-        })
57
-
58
-        elm.innerText = shape.text
59
-        updateShapeSize()
60
-
61
-        return () => {
62
-          observer.disconnect()
63
-        }
64
-      }, [])
65
-
66
-      React.useEffect(() => {
67
-        if (isEditing) {
68
-          rInput.current!.focus()
69
-        }
70
-      }, [isEditing])
71
-
72
-      return (
73
-        <HTMLContainer ref={ref}>
74
-          <div
75
-            {...events}
76
-            style={{
77
-              pointerEvents: 'all',
78
-              width: shape.size[0],
79
-              height: shape.size[1],
80
-              display: 'flex',
81
-              fontSize: 20,
82
-              fontFamily: 'sans-serif',
83
-              alignItems: 'center',
84
-              justifyContent: 'center',
85
-              border: `2px solid ${color}`,
86
-              color,
87
-            }}
88
-          >
89
-            <div onPointerDown={(e) => isEditing && e.stopPropagation()}>
90
-              <div
91
-                ref={rInput}
92
-                style={{
93
-                  whiteSpace: 'nowrap',
94
-                  overflow: 'hidden',
95
-                  textAlign: 'center',
96
-                  outline: 'none',
97
-                  userSelect: isEditing ? 'all' : 'none',
98
-                }}
99
-                contentEditable={isEditing}
100
-              />
101
-            </div>
102
-          </div>
103
-        </HTMLContainer>
104
-      )
105
-    }
106
-  )
107
-
108
-  Indicator = TLShapeUtil.Indicator<BoxShape>(({ shape }) => {
109
-    return (
110
-      <SVGContainer>
111
-        <rect
112
-          fill="none"
113
-          stroke="blue"
114
-          strokeWidth={1}
115
-          width={shape.size[0]}
116
-          height={shape.size[1]}
117
-          pointerEvents="none"
118
-        />
119
-      </SVGContainer>
120
-    )
121
-  })
122
-
123
-  getBounds = (shape: BoxShape) => {
124
-    const bounds = Utils.getFromCache(this.boundsCache, shape, () => {
125
-      const [width, height] = shape.size
126
-      return {
127
-        minX: 0,
128
-        maxX: width,
129
-        minY: 0,
130
-        maxY: height,
131
-        width,
132
-        height,
133
-      } as TLBounds
134
-    })
135
-
136
-    return Utils.translateBounds(bounds, shape.point)
137
-  }
138
-}

+ 0
- 35
packages/dev/src/core/index.tsx 파일 보기

@@ -1,35 +0,0 @@
1
-import * as React from 'react'
2
-import { Renderer, TLShapeUtilsMap } from '@tldraw/core'
3
-import { BoxShape, BoxUtil } from './box'
4
-import { LabelUtil, LabelShape } from './label'
5
-import { appState } from './state'
6
-
7
-const shapeUtils: TLShapeUtilsMap<BoxShape | LabelShape> = {
8
-  box: new BoxUtil(),
9
-  label: new LabelUtil(),
10
-}
11
-
12
-export default function Core() {
13
-  const page = appState.useStore((s) => s.page)
14
-  const pageState = appState.useStore((s) => s.pageState)
15
-  const meta = appState.useStore((s) => s.meta)
16
-
17
-  return (
18
-    <div className="tldraw">
19
-      <Renderer
20
-        shapeUtils={shapeUtils}
21
-        page={page}
22
-        pageState={pageState}
23
-        meta={meta}
24
-        onDoubleClickBounds={appState.onDoubleClickBounds}
25
-        onDoubleClickShape={appState.onDoubleClickShape}
26
-        onPointShape={appState.onPointShape}
27
-        onPointCanvas={appState.onPointCanvas}
28
-        onPointerDown={appState.onPointerDown}
29
-        onPointerMove={appState.onPointerMove}
30
-        onPointerUp={appState.onPointerUp}
31
-        onShapeChange={appState.onShapeChange}
32
-      />
33
-    </div>
34
-  )
35
-}

+ 0
- 124
packages/dev/src/core/label.tsx 파일 보기

@@ -1,124 +0,0 @@
1
-/* eslint-disable @typescript-eslint/no-non-null-assertion */
2
-/* refresh-reset */
3
-
4
-import * as React from 'react'
5
-import {
6
-  TLShape,
7
-  Utils,
8
-  TLBounds,
9
-  HTMLContainer,
10
-  TLComponent,
11
-  TLShapeUtil,
12
-  TLIndicator,
13
-} from '@tldraw/core'
14
-import { appState } from './state'
15
-
16
-// Define a custom shape
17
-
18
-export interface LabelShape extends TLShape {
19
-  type: 'label'
20
-  text: string
21
-}
22
-
23
-// Create a "shape utility" class that interprets that shape
24
-
25
-export class LabelUtil extends TLShapeUtil<LabelShape, HTMLDivElement> {
26
-  type = 'label' as const
27
-
28
-  isStateful = true
29
-
30
-  Component: TLComponent<LabelShape, HTMLDivElement> = (
31
-    { shape, events, isSelected, onShapeChange, meta },
32
-    ref
33
-  ) => {
34
-    const color = meta.isDarkMode ? 'white' : 'black'
35
-    const bounds = this.getBounds(shape)
36
-    const rInput = React.useRef<HTMLDivElement>(null)
37
-    function updateShapeSize() {
38
-      const elm = rInput.current!
39
-      appState.changeShapeText(shape.id, elm.innerText)
40
-      onShapeChange?.({
41
-        id: shape.id,
42
-        text: elm.innerText,
43
-      })
44
-    }
45
-    React.useLayoutEffect(() => {
46
-      const elm = rInput.current!
47
-      elm.innerText = shape.text
48
-      updateShapeSize()
49
-      const observer = new MutationObserver(updateShapeSize)
50
-      observer.observe(elm, {
51
-        attributes: true,
52
-        characterData: true,
53
-        subtree: true,
54
-      })
55
-    }, [])
56
-    return (
57
-      <HTMLContainer>
58
-        <div
59
-          {...events}
60
-          style={{
61
-            width: bounds.width,
62
-            height: bounds.height,
63
-            pointerEvents: 'all',
64
-            display: 'flex',
65
-            fontSize: 20,
66
-            fontFamily: 'sans-serif',
67
-            alignItems: 'center',
68
-            justifyContent: 'center',
69
-            border: `2px solid ${color}`,
70
-            color,
71
-          }}
72
-        >
73
-          <div ref={ref} onPointerDown={(e) => isSelected && e.stopPropagation()}>
74
-            <div
75
-              ref={rInput}
76
-              style={{
77
-                whiteSpace: 'nowrap',
78
-                overflow: 'hidden',
79
-                textAlign: 'center',
80
-                outline: 'none',
81
-                userSelect: isSelected ? 'all' : 'none',
82
-              }}
83
-              contentEditable={isSelected}
84
-            />
85
-          </div>
86
-        </div>
87
-      </HTMLContainer>
88
-    )
89
-  }
90
-
91
-  Indicator: TLIndicator<LabelShape> = ({ shape }) => {
92
-    const bounds = this.getBounds(shape)
93
-
94
-    return (
95
-      <rect
96
-        fill="none"
97
-        stroke="blue"
98
-        strokeWidth={1}
99
-        width={bounds.width}
100
-        height={bounds.height}
101
-        pointerEvents="none"
102
-      />
103
-    )
104
-  }
105
-
106
-  getBounds = (shape: LabelShape) => {
107
-    const bounds = Utils.getFromCache(this.boundsCache, shape, () => {
108
-      const ref = this.getRef(shape)
109
-      const width = ref.current?.offsetWidth || 0
110
-      const height = ref.current?.offsetHeight || 0
111
-
112
-      return {
113
-        minX: 0,
114
-        maxX: width,
115
-        minY: 0,
116
-        maxY: height,
117
-        width,
118
-        height,
119
-      } as TLBounds
120
-    })
121
-
122
-    return Utils.translateBounds(bounds, shape.point)
123
-  }
124
-}

+ 0
- 153
packages/dev/src/core/state.ts 파일 보기

@@ -1,153 +0,0 @@
1
-import type {
2
-  TLBinding,
3
-  TLPage,
4
-  TLPageState,
5
-  TLPointerEventHandler,
6
-  TLShapeChangeHandler,
7
-} from '@tldraw/core'
8
-import type { BoxShape } from './box'
9
-import type { LabelShape } from './label'
10
-import { StateManager } from 'rko'
11
-
12
-type Shapes = BoxShape | LabelShape
13
-
14
-interface State {
15
-  page: TLPage<Shapes, TLBinding>
16
-  pageState: TLPageState
17
-  meta: {
18
-    isDarkMode: boolean
19
-  }
20
-}
21
-
22
-class AppState extends StateManager<State> {
23
-  /* ----------------------- API ---------------------- */
24
-
25
-  selectShape(shapeId: string) {
26
-    this.patchState({
27
-      pageState: {
28
-        selectedIds: [shapeId],
29
-      },
30
-    })
31
-  }
32
-
33
-  deselect() {
34
-    this.patchState({
35
-      pageState: {
36
-        selectedIds: [],
37
-        editingId: undefined,
38
-      },
39
-    })
40
-  }
41
-
42
-  startEditingShape(shapeId: string) {
43
-    this.patchState({
44
-      pageState: {
45
-        selectedIds: [shapeId],
46
-        editingId: shapeId,
47
-      },
48
-    })
49
-  }
50
-
51
-  changeShapeText = (id: string, text: string) => {
52
-    this.patchState({
53
-      page: {
54
-        shapes: {
55
-          [id]: { text },
56
-        },
57
-      },
58
-    })
59
-  }
60
-
61
-  /* --------------------- Events --------------------- */
62
-
63
-  onPointCanvas: TLPointerEventHandler = (info) => {
64
-    this.deselect()
65
-  }
66
-
67
-  onPointShape: TLPointerEventHandler = (info) => {
68
-    this.selectShape(info.target)
69
-  }
70
-
71
-  onDoubleClickShape: TLPointerEventHandler = (info) => {
72
-    this.startEditingShape(info.target)
73
-  }
74
-
75
-  onDoubleClickBounds: TLPointerEventHandler = (info) => {
76
-    // Todo
77
-  }
78
-
79
-  onPointerDown: TLPointerEventHandler = (info) => {
80
-    // Todo
81
-  }
82
-
83
-  onPointerUp: TLPointerEventHandler = (info) => {
84
-    // Todo
85
-  }
86
-
87
-  onPointerMove: TLPointerEventHandler = (info) => {
88
-    // Todo
89
-  }
90
-
91
-  onShapeChange: TLShapeChangeHandler<Shapes> = (shape) => {
92
-    if (shape.type === 'box' && shape.size) {
93
-      this.patchState({
94
-        page: {
95
-          shapes: {
96
-            [shape.id]: { ...shape, size: [...shape.size] },
97
-          },
98
-        },
99
-      })
100
-    }
101
-  }
102
-}
103
-
104
-export const appState = new AppState({
105
-  page: {
106
-    id: 'page1',
107
-    shapes: {
108
-      rect1: {
109
-        id: 'rect1',
110
-        parentId: 'page1',
111
-        name: 'Rectangle',
112
-        childIndex: 1,
113
-        type: 'box',
114
-        point: [0, 0],
115
-        rotation: 0,
116
-        size: [100, 100],
117
-        text: 'Hello world!',
118
-      },
119
-      label1: {
120
-        id: 'label1',
121
-        parentId: 'page1',
122
-        name: 'Label',
123
-        childIndex: 2,
124
-        type: 'label',
125
-        point: [-200, -200],
126
-        rotation: 0,
127
-        text: 'My shape is stateful, I should still render while off screen!',
128
-      },
129
-      label2: {
130
-        id: 'label2',
131
-        parentId: 'page1',
132
-        name: 'Label',
133
-        childIndex: 2,
134
-        type: 'label',
135
-        point: [200, 200],
136
-        rotation: 0,
137
-        text: 'Hello world!',
138
-      },
139
-    },
140
-    bindings: {},
141
-  },
142
-  pageState: {
143
-    id: 'page1',
144
-    selectedIds: [],
145
-    camera: {
146
-      point: [0, 0],
147
-      zoom: 1,
148
-    },
149
-  },
150
-  meta: {
151
-    isDarkMode: false,
152
-  },
153
-})

Loading…
취소
저장