Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 16, 2022
1 parent ce2b6eb commit f34ba4b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 61 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/models/MoveHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class MoveHelper {
this.makeObservable()
}

get cursor() {
return this.operation.engine.cursor
}

get viewport() {
return this.operation.workspace.viewport
}
Expand Down Expand Up @@ -274,7 +278,8 @@ export class MoveHelper {
source: this.dragNodes,
})
)
this.operation.engine.cursor.setDragType(CursorDragType.Move)
this.viewport.cacheElements()
this.cursor.setDragType(CursorDragType.Move)
this.dragging = true
}
}
Expand Down Expand Up @@ -349,6 +354,7 @@ export class MoveHelper {
this.viewportClosestDirection = null
this.viewportClosestOffsetRect = null
this.viewportClosestRect = null
this.viewport.clearCache()
}

trigger(event: any) {
Expand Down
52 changes: 0 additions & 52 deletions packages/core/src/models/NodeSelector.ts

This file was deleted.

37 changes: 29 additions & 8 deletions packages/core/src/models/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { action, define, observable } from '@formily/reactive'
import { Workspace } from './Workspace'
import { Engine } from './Engine'
import { TreeNode } from './TreeNode'
import { NodeSelector } from './NodeSelector'

export interface IViewportProps {
engine: Engine
Expand All @@ -42,8 +41,6 @@ export type IViewportMoveInsertionType = 'all' | 'inline' | 'block'
export class Viewport {
workspace: Workspace

selector: NodeSelector

engine: Engine

contentWindow: Window
Expand All @@ -70,6 +67,8 @@ export class Viewport {

moveInsertionType: IViewportMoveInsertionType

nodeElementsStore: Record<string, HTMLElement[]> = {}

constructor(props: IViewportProps) {
this.workspace = props.workspace
this.engine = props.engine
Expand All @@ -78,7 +77,6 @@ export class Viewport {
this.viewportElement = props.viewportElement
this.contentWindow = props.contentWindow
this.nodeIdAttrName = props.nodeIdAttrName
this.selector = new NodeSelector()
this.digestViewport()
this.makeObservable()
this.attachEvents()
Expand Down Expand Up @@ -178,6 +176,21 @@ export class Viewport {
return this.scrollY - this.dragStartSnapshot.scrollY
}

cacheElements() {
this.nodeElementsStore = {}
this.viewportRoot
?.querySelectorAll(`*[${this.nodeIdAttrName}]`)
.forEach((element: HTMLElement) => {
const id = element.getAttribute(this.nodeIdAttrName)
this.nodeElementsStore[id] = this.nodeElementsStore[id] || []
this.nodeElementsStore[id].push(element)
})
}

clearCache() {
this.nodeElementsStore = {}
}

getCurrentData() {
const data: IViewportData = {}
if (this.isIframe) {
Expand Down Expand Up @@ -305,14 +318,22 @@ export class Viewport {
})
}

findElementById(id: string) {
findElementById(id: string): HTMLElement {
if (!id) return
return this.selector.query(this.viewportRoot, this.nodeIdAttrName, id)
if (this.nodeElementsStore[id]) return this.nodeElementsStore[id][0]
return this.viewportRoot?.querySelector(
`*[${this.nodeIdAttrName}='${id}']`
) as HTMLElement
}

findElementsById(id: string) {
findElementsById(id: string): HTMLElement[] {
if (!id) return []
return this.selector.queryAll(this.viewportRoot, this.nodeIdAttrName, id)
if (this.nodeElementsStore[id]) return this.nodeElementsStore[id]
return Array.from(
this.viewportRoot?.querySelectorAll(
`*[${this.nodeIdAttrName}='${id}']`
) ?? []
)
}

containsElement(element: HTMLElement | Element | EventTarget) {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const calcElementOuterWidth = (
export const calcElementLayout = (element: Element) => {
if (!element) return 'vertical'
const parent = element.parentElement
if (!parent) return 'vertical'
const tagName = element.tagName
const parentTagName = parent.tagName
const style = getComputedStyle(element)
Expand Down

0 comments on commit f34ba4b

Please sign in to comment.