mirror of
https://github.com/idrawjs/idraw
synced 2026-05-24 01:58:27 +00:00
feat: add helper to core draw
This commit is contained in:
parent
9906df52e3
commit
8592197491
4 changed files with 53 additions and 18 deletions
|
|
@ -2,6 +2,7 @@ import { TypeData, TypePoint } from '@idraw/types';
|
|||
import Board from '@idraw/board';
|
||||
import Renderer from './lib/renderer';
|
||||
import { Element } from './lib/element';
|
||||
import { Helper } from './lib/helper';
|
||||
|
||||
type Options = {
|
||||
width: number;
|
||||
|
|
@ -22,6 +23,7 @@ class Core {
|
|||
private _opts: Options;
|
||||
private _renderer: Renderer;
|
||||
private _element: Element;
|
||||
private _helper: Helper;
|
||||
private _hasInited: boolean = false;
|
||||
private _mode: Mode = Mode.NULL;
|
||||
|
||||
|
|
@ -34,12 +36,14 @@ class Core {
|
|||
this._board = new Board(mount, this._opts);
|
||||
this._renderer = new Renderer(this._board);
|
||||
this._element = new Element(this._board.getContext());
|
||||
this._helper = new Helper();
|
||||
this._initEvent();
|
||||
this._hasInited = true;
|
||||
}
|
||||
|
||||
draw() {
|
||||
this._renderer.render(this._data);
|
||||
this._helper.updateConfig(this._data, { selectedUUID: this._selectedUUID });
|
||||
this._renderer.render(this._data, this._helper.getConfig());
|
||||
}
|
||||
|
||||
selectElement(index: number) {
|
||||
|
|
|
|||
|
|
@ -3,20 +3,39 @@ import {
|
|||
TypeHelper,
|
||||
TypeHelperConfig,
|
||||
TypeHelperCreateOpts,
|
||||
TypeElement,
|
||||
TypeElemDesc
|
||||
} from '@idraw/types';
|
||||
|
||||
export class Helper implements TypeHelper {
|
||||
|
||||
constructor() {}
|
||||
private _config: TypeHelperConfig;
|
||||
|
||||
createConfig<T extends keyof TypeElemDesc>(
|
||||
data: TypeData,
|
||||
opts: TypeHelperCreateOpts ): TypeHelperConfig<T> {
|
||||
const config = {
|
||||
elementMap: {}
|
||||
constructor() {
|
||||
this._config = {
|
||||
elementIndexMap: {},
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
}
|
||||
|
||||
updateConfig (
|
||||
data: TypeData,
|
||||
opts: TypeHelperCreateOpts ) {
|
||||
this._updateElementIndex(data);
|
||||
}
|
||||
|
||||
getConfig() {
|
||||
// TODO
|
||||
return JSON.parse(JSON.stringify(this._config));
|
||||
}
|
||||
|
||||
private _updateElementIndex(data: TypeData) {
|
||||
this._config.elementIndexMap = {};
|
||||
data.elements.forEach((elem: TypeElement<keyof TypeElemDesc>, i) => {
|
||||
this._config.elementIndexMap[elem.uuid] = i;
|
||||
});
|
||||
}
|
||||
|
||||
// private _updateSelectedElementWrapper() {
|
||||
|
||||
// }
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { TypeContext, TypeData } from '@idraw/types';
|
||||
import { TypeContext, TypeData, TypeHelperConfig } from '@idraw/types';
|
||||
import { drawContext } from './draw';
|
||||
import Board from '@idraw/board';
|
||||
|
||||
|
|
@ -13,7 +13,7 @@ export default class Renderer {
|
|||
this._ctx = this._board.getContext();
|
||||
}
|
||||
|
||||
render(data: TypeData) {
|
||||
render(data: TypeData, config?: TypeHelperConfig) {
|
||||
this._data = data;
|
||||
drawContext(this._ctx, this._data);
|
||||
this._board.draw();
|
||||
|
|
|
|||
|
|
@ -1,21 +1,33 @@
|
|||
import { TypeData } from './data';
|
||||
import { TypeElement, TypeElemDesc } from './element';
|
||||
import { TypePoint } from './board';
|
||||
|
||||
// type test = {[uuid string]: TypeElement}
|
||||
|
||||
type TypeHelperConfig<T extends keyof TypeElemDesc> = {
|
||||
elementMap: {[key: string]: TypeElement<T>}
|
||||
type TypeHelperConfig = {
|
||||
elementIndexMap: {[key: string]: Number},
|
||||
selectedElementWrapper?: {
|
||||
size: number;
|
||||
topLeft: TypePoint,
|
||||
top: TypePoint,
|
||||
topRight: TypePoint,
|
||||
right: TypePoint,
|
||||
bottomRight: TypePoint,
|
||||
bottom: TypePoint,
|
||||
bottomLeft: TypePoint,
|
||||
left: TypePoint,
|
||||
}
|
||||
}
|
||||
|
||||
type TypeHelperCreateOpts = {
|
||||
selectedIndex: number
|
||||
selectedUUID?: string | null,
|
||||
}
|
||||
|
||||
interface TypeHelper {
|
||||
createConfig<T extends keyof TypeElemDesc>(
|
||||
updateConfig(
|
||||
data: TypeData,
|
||||
opts: TypeHelperCreateOpts
|
||||
): TypeHelperConfig<T>;
|
||||
): void;
|
||||
getConfig(): TypeHelperConfig;
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
|||
Loading…
Reference in a new issue