Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin refactoring prefinal #33

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { REALTY_SCENE, REALTY_SCENE_1 } from './mocks';
let isDarkTheme = false;

async function start() {
const mapglAPI = await load('https://mapgl.2gis.com/api/js/v0.0.322');
const mapglAPI = await load();

const map = new mapglAPI.Map('container', {
center: [47.245286302641034, 56.134743473834099],
Expand All @@ -20,19 +20,11 @@ async function start() {
(window as any).map = map;

const plugin = new GltfPlugin(map, {
modelsLoadStrategy: 'waitAll',
modelsLoadStrategy: 'dontWaitAll',
modelsBaseUrl: 'https://disk.2gis.com/digital-twin/models_s3/realty_ads/zgktechnology/',
floorsControl: { position: 'centerRight' },
poiConfig: {
primary: {
fontSize: 14,
},
secondary: {
fontSize: 14,
},
},
hoverHighlight: {
intencity: 0.1,
hoverOptions: {
color: '#FFF3F3',
},
groundCoveringColor: 'rgba(0, 0, 0, 0.8)',
});
Expand Down
139 changes: 79 additions & 60 deletions demo/mocks.ts

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"author": "2GIS WebMaps Team",
"license": "BSD-2-Clause",
"devDependencies": {
"@2gis/mapgl": "1.37.2",
"@2gis/mapgl": "1.46.0",
"@documentalist/compiler": "^2.8.1",
"@types/geojson": "^7946.0.10",
"@types/jest": "^27.4.0",
Expand Down
9 changes: 7 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { GeoJsonSourceOptions } from '@2gis/mapgl/types';

export const PLUGIN_PREFIX = '__mapglPlugins_mapgl-gltf2';

export const GROUND_COVERING_SOURCE_DATA: GeoJsonSourceOptions['data'] = {
type: 'Feature',
properties: {},
Expand All @@ -17,9 +19,10 @@ export const GROUND_COVERING_SOURCE_DATA: GeoJsonSourceOptions['data'] = {
},
};

export const GROUND_COVERING_SOURCE_PURPOSE = '__mapglPlugins_mapgl-gltf';
export const GROUND_COVERING_SOURCE_PURPOSE = `${PLUGIN_PREFIX}-covering`;
export const GROUND_COVERING_LAYER_ID = `${PLUGIN_PREFIX}-covering`;
export const GROUND_COVERING_LAYER = {
id: '__mapglPlugins_mapgl-gltf',
id: GROUND_COVERING_LAYER_ID,
type: 'polygon',
style: {
color: ['to-color', ['sourceAttr', 'color']],
Expand All @@ -30,3 +33,5 @@ export const GROUND_COVERING_LAYER = {
['to-boolean', ['sourceAttr', 'color']],
],
};

export const pluginEvents = ['click', 'mousemove', 'mouseover', 'mouseout'] as const;
7 changes: 3 additions & 4 deletions src/control/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import icon_building from 'raw-loader!./icon_building.svg';
import icon_parking from 'raw-loader!./icon_parking.svg';
import classes from './control.module.css';
import { Control } from './control';
import { Id } from '../types';

const content = /* HTML */ `
<div class="${classes.root}">
Expand All @@ -23,7 +22,7 @@ const content = /* HTML */ `
`;

/**
* A control for change floor layer level on the plugin.
* A control for change floor layer level in the plugin.
* It appears on the map only if you set the `floorControl` option within @type PluginOptions to `true`.
* @hidden
* @internal
Expand Down Expand Up @@ -120,15 +119,15 @@ export class GltfFloorControl extends Control {
});
}

private _controlHandler = (modelId: Id) => () => {
private _controlHandler = (modelId: string) => () => {
this._switchCurrentFloorLevel(modelId);

this.emit('floorchange', {
modelId,
});
};

private _switchCurrentFloorLevel(modelId: Id) {
private _switchCurrentFloorLevel(modelId: string) {
if (this._currentFloorId === undefined) {
return;
}
Expand Down
18 changes: 8 additions & 10 deletions src/control/types.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { Id } from '../types/plugin';

/**
* Floor level data
* Floor level data.
*/
export interface FloorLevel {
modelId: Id; // id модели этажа или здания
modelId: string; // id модели этажа или здания
text: string;
icon?: 'parking' | 'building' | string;
}

/**
* Options for the method show
* Options for the show method.
*/
export interface ControlShowOptions {
buildingModelId: Id;
activeModelId: Id;
buildingModelId: string;
activeModelId: string;
floorLevels?: FloorLevel[];
}

/**
* Event that emitted on button presses of the control
* Event that emitted on button presses of the control.
*/
export interface FloorChangeEvent {
modelId: Id; // id модели этажа или здания
modelId: string; // id модели этажа или здания
}

export interface ControlEventTable {
/**
* Emitted when floor's plan was changed
* Emitted when floor's plan was changed.
*/
floorchange: FloorChangeEvent;
}
13 changes: 1 addition & 12 deletions src/defaultOptions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import type { PluginOptions } from './types/plugin';

export const defaultOptions: Required<PluginOptions> = {
hoverHighlight: {
hoverOptions: {
color: '#ffffff',
intencity: 0.0,
},
modelsBaseUrl: '',
modelsLoadStrategy: 'waitAll',
poiConfig: {
primary: {
fontSize: 14,
fontColor: '#000000',
},
secondary: {
fontSize: 14,
fontColor: '#000000',
},
},
floorsControl: {
position: 'centerLeft',
},
Expand Down
30 changes: 17 additions & 13 deletions src/external/evented.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Event emitter
* Event emitter.
*/
export class Evented<M> {
private events: { [K in keyof M]?: Array<(ev: M[K]) => void> };
Expand All @@ -12,9 +12,10 @@ export class Evented<M> {
}

/**
* Registers event listener
* @param type Event type
* @param listener Event handler
* Registers event listener.
*
* @param type Event type.
* @param listener Event handler.
*/
public on<K extends keyof M>(type: K, listener: (ev: M[K]) => void): this {
let eventsByType = this.events[type];
Expand All @@ -26,9 +27,10 @@ export class Evented<M> {
}

/**
* Registers event listener which will be called once
* @param type Event type
* @param listener Event handler
* Registers event listener which will be called once.
*
* @param type Event type.
* @param listener Event handler.
*/
public once<K extends keyof M>(type: K, listener: (ev: M[K]) => void): this {
const wrapper = (data: M[K]) => {
Expand All @@ -42,9 +44,10 @@ export class Evented<M> {
}

/**
* Removes event listener registered with `on`
* @param type Event type
* @param listener Event handler
* Removes event listener registered with `on`.
*
* @param type Event type.
* @param listener Event handler.
*/
public off<K extends keyof M>(type: K, listener: (ev: M[K]) => void): this {
const eventsByType = this.events[type];
Expand All @@ -63,9 +66,10 @@ export class Evented<M> {
}

/**
* Calls all event listeners with event type `type`
* @param type Event type
* @param data Data transferred to events
* Calls all event listeners with event type `type`.
*
* @param type Event type.
* @param data Data transferred to events.
*/
public emit<K extends keyof M>(type: K, data?: M[K]): this {
const eventsByType = this.events[type];
Expand Down
61 changes: 61 additions & 0 deletions src/labelGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Map as MapGL, Label } from '@2gis/mapgl/types';
import type { BuildingState, LabelGroupOptions } from './types/plugin';
import type { GltfPlugin } from './plugin';
// import { pluginEvents } from './constants';
// import { createLabelEvenData } from './utils/events';

export class LabelGroups {
private labelsByGroupId: Map<string, Label[]> = new Map();

constructor(private map: MapGL, private plugin: GltfPlugin) {}

public add(groupOptions: LabelGroupOptions, state?: BuildingState) {
const { id } = groupOptions;
if (this.labelsByGroupId.has(id)) {
console.error(
`Poi group with id "${id}" already exists. Please use different identifiers for poi groups`,
);
return;
}

const { image, minZoom, maxZoom, fontColor: color, fontSize } = groupOptions;
const labels = groupOptions.labels.map((labelOptions) => {
const { coordinates, text, userData } = labelOptions;
const label = new mapgl.Label(this.map, {
coordinates, // + label.elevation ?? groupOptions.elevation
alxart marked this conversation as resolved.
Show resolved Hide resolved
text,
userData,
image,
minZoom,
maxZoom,
color,
fontSize,
relativeAnchor: [0.5, 1],
zIndex: 1, // чтобы были выше моделей
kalyanov marked this conversation as resolved.
Show resolved Hide resolved
});

// pluginEvents.forEach((eventType) => {
// label.on(eventType, (ev) => {
// this.plugin.emit(eventType, createLabelEvenData(ev, labelOptions, state));
// });
// });
alxart marked this conversation as resolved.
Show resolved Hide resolved

return label;
});

this.labelsByGroupId.set(id, labels);
}

public remove(id: string) {
const labels = this.labelsByGroupId.get(id);
this.labelsByGroupId.delete(id);
labels?.forEach((label) => label.destroy());
}

public destroy() {
this.labelsByGroupId.forEach((labels) => {
labels.forEach((label) => label.destroy());
});
this.labelsByGroupId.clear();
}
}
Loading
Loading