@leafer/canvas-web 1.0.0-rc.8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/canvas-web",
3
- "version": "1.0.0-rc.8",
3
+ "version": "1.0.0",
4
4
  "description": "@leafer/canvas-web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.0.0-rc.8"
25
+ "@leafer/core": "1.0.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-rc.8"
28
+ "@leafer/interface": "1.0.0"
29
29
  }
30
30
  }
@@ -1,5 +1,5 @@
1
- import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener, ICursorType } from '@leafer/interface'
2
- import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug, Cursor } from '@leafer/core'
1
+ import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
2
+ import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug } from '@leafer/core'
3
3
 
4
4
 
5
5
  const debug = Debug.get('LeaferCanvas')
@@ -9,6 +9,25 @@ export class LeaferCanvas extends LeaferCanvasBase {
9
9
  declare public view: HTMLCanvasElement
10
10
  declare public parentView: HTMLElement
11
11
 
12
+ public set zIndex(zIndex: number) {
13
+ const { style } = this.view
14
+ style.zIndex = zIndex as unknown as string
15
+ this.setAbsolute(this.view)
16
+ }
17
+
18
+ public set childIndex(index: number) {
19
+ const { view, parentView } = this
20
+ if (view && parentView) {
21
+ const beforeNode = parentView.children[index]
22
+ if (beforeNode) {
23
+ this.setAbsolute(beforeNode as HTMLCanvasElement)
24
+ parentView.insertBefore(view, beforeNode)
25
+ } else {
26
+ parentView.appendChild(beforeNode)
27
+ }
28
+ }
29
+ }
30
+
12
31
  protected resizeObserver: ResizeObserver
13
32
  protected autoBounds: IAutoBounds
14
33
  protected resizeListener: IResizeEventListener
@@ -19,11 +38,15 @@ export class LeaferCanvas extends LeaferCanvasBase {
19
38
  view ? this.__createViewFrom(view) : this.__createView()
20
39
  const { style } = this.view
21
40
  style.display || (style.display = 'block')
22
- style.userSelect = 'none'
23
41
  this.parentView = this.view.parentElement
24
42
 
43
+ if (this.parentView) {
44
+ const pStyle = this.parentView.style
45
+ pStyle.webkitUserSelect = pStyle.userSelect = 'none' // fix safari: use webkitUserSelect
46
+ }
47
+
25
48
  if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
26
- this.view.style.display = 'none'
49
+ style.display = 'none'
27
50
  document.body.appendChild(this.view)
28
51
  }
29
52
 
@@ -42,27 +65,6 @@ export class LeaferCanvas extends LeaferCanvasBase {
42
65
  this.view = document.createElement('canvas')
43
66
  }
44
67
 
45
- public setCursor(cursor: ICursorType | ICursorType[]): void {
46
- const list: ICursorType[] = []
47
- this.eachCursor(cursor, list)
48
- if (typeof list[list.length - 1] === 'object') list.push('default')
49
- this.view.style.cursor = list.map(item => (typeof item === 'object') ? `url(${item.url}) ${item.x || 0} ${item.y || 0}` : item).join(',')
50
- }
51
-
52
- protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level = 0): void {
53
- level++
54
- if (cursor instanceof Array) {
55
- cursor.forEach(item => this.eachCursor(item, list, level))
56
- } else {
57
- const custom = typeof cursor === 'string' && Cursor.get(cursor)
58
- if (custom && level < 2) {
59
- this.eachCursor(custom, list, level)
60
- } else {
61
- list.push(cursor)
62
- }
63
- }
64
- }
65
-
66
68
  protected __createViewFrom(inputView: string | object): void {
67
69
  let find: unknown = (typeof inputView === 'string') ? document.getElementById(inputView) : inputView as HTMLElement
68
70
  if (find) {
@@ -86,9 +88,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
86
88
  const view = this.view
87
89
 
88
90
  if (parent.hasChildNodes()) {
89
- const { style } = view
90
- style.position = 'absolute'
91
- style.top = style.left = '0px'
91
+ this.setAbsolute(view)
92
92
  parent.style.position || (parent.style.position = 'relative')
93
93
  }
94
94
 
@@ -100,6 +100,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
100
100
  }
101
101
  }
102
102
 
103
+ protected setAbsolute(view: HTMLCanvasElement): void {
104
+ const { style } = view
105
+ style.position = 'absolute'
106
+ style.top = style.left = '0px'
107
+ }
108
+
103
109
  public updateViewSize(): void {
104
110
  const { width, height, pixelRatio } = this
105
111
 
@@ -107,8 +113,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
107
113
  style.width = width + 'px'
108
114
  style.height = height + 'px'
109
115
 
110
- this.view.width = width * pixelRatio
111
- this.view.height = height * pixelRatio
116
+ this.view.width = Math.ceil(width * pixelRatio)
117
+ this.view.height = Math.ceil(height * pixelRatio)
112
118
  }
113
119
 
114
120
  public updateClientBounds(): void {
@@ -129,6 +135,9 @@ export class LeaferCanvas extends LeaferCanvasBase {
129
135
  if (parent) {
130
136
  this.resizeObserver.observe(parent)
131
137
  this.checkAutoBounds(parent.getBoundingClientRect())
138
+ } else {
139
+ this.checkAutoBounds(this.view)
140
+ debug.warn('no parent')
132
141
  }
133
142
 
134
143
  } catch {
package/types/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
- import { IAutoBounds, IResizeEventListener, ICursorType, ISizeData } from '@leafer/interface';
1
+ import { IAutoBounds, IResizeEventListener, ISizeData } from '@leafer/interface';
2
2
  import { LeaferCanvasBase } from '@leafer/core';
3
3
 
4
4
  declare class LeaferCanvas extends LeaferCanvasBase {
5
5
  view: HTMLCanvasElement;
6
6
  parentView: HTMLElement;
7
+ set zIndex(zIndex: number);
8
+ set childIndex(index: number);
7
9
  protected resizeObserver: ResizeObserver;
8
10
  protected autoBounds: IAutoBounds;
9
11
  protected resizeListener: IResizeEventListener;
@@ -13,9 +15,8 @@ declare class LeaferCanvas extends LeaferCanvasBase {
13
15
  set hittable(hittable: boolean);
14
16
  get hittable(): boolean;
15
17
  protected __createView(): void;
16
- setCursor(cursor: ICursorType | ICursorType[]): void;
17
- protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level?: number): void;
18
18
  protected __createViewFrom(inputView: string | object): void;
19
+ protected setAbsolute(view: HTMLCanvasElement): void;
19
20
  updateViewSize(): void;
20
21
  updateClientBounds(): void;
21
22
  startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;