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

feat: add filterTargetData method and select event #260

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createBoardContent, validateElements } from '@idraw/util';
import { Cursor } from './lib/cursor';

// export { MiddlewareSelector } from './middleware/selector';
export { MiddlewareSelector, middlewareEventSelect } from './middleware/selector';
export { MiddlewareSelector, middlewareEventSelect, middlewareEventSelectClear } from './middleware/selector';
export { MiddlewareScroller } from './middleware/scroller';
export { MiddlewareScaler, middlewareEventScale } from './middleware/scaler';
export { MiddlewareRuler, middlewareEventRuler } from './middleware/ruler';
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/middleware/selector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import { middlewareEventTextEdit } from '../text-editor';

export const middlewareEventSelect: string = '@middleware/select';

export const middlewareEventSelectClear: string = '@middleware/select-clear';

export const MiddlewareSelector: BoardMiddleware<DeepSelectorSharedStorage, CoreEvent> = (opts) => {
const { viewer, sharer, boardContent, calculator, eventHub } = opts;
const { helperContext } = boardContent;
Expand Down Expand Up @@ -175,13 +177,20 @@ export const MiddlewareSelector: BoardMiddleware<DeepSelectorSharedStorage, Core
}
};

const selectClearCallback = () => {
clear();
viewer.drawFrame();
};

return {
use() {
eventHub.on(middlewareEventSelect, selectCallback);
eventHub.on(middlewareEventSelectClear, selectClearCallback);
},

disuse() {
eventHub.off(middlewareEventSelect, selectCallback);
eventHub.off(middlewareEventSelectClear, selectClearCallback);
},

hover: (e: PointWatcherEvent) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/idraw/src/idraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
deleteElementInList,
moveElementPosition,
getElementPositionFromList,
calcElementListSize
calcElementListSize,
filterCompactData
} from '@idraw/util';
import { defaultSettings } from './config';
import { exportImageFileBlobURL } from './file';
Expand Down Expand Up @@ -111,8 +112,14 @@ export class iDraw {
core.trigger('change', { data, type: 'set-data' });
}

getData(): Data | null {
return this.#core.getData();
getData(opts?: { compact?: boolean }): Data | null {
const data = this.#core.getData();
if (data && opts?.compact === true) {
return filterCompactData(data, {
loadItemMap: this.#core.getLoadItemMap()
});
}
return data;
}

getViewInfo(): {
Expand Down
1 change: 1 addition & 0 deletions packages/idraw/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
Core,
MiddlewareSelector,
middlewareEventSelect,
middlewareEventSelectClear,
MiddlewareScroller,
MiddlewareScaler,
middlewareEventScale,
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface CoreEventSelect {
positions?: Array<Array<number>>;
}
export interface CoreEventChange {
type: string;
data: Data;
}
export interface CoreEventScale {
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { delay, compose, throttle } from './lib/time';
export { downloadImageFromCanvas, parseFileToBase64, pickFile, parseFileToText, downloadFileFromText } from './lib/file';
export { toColorHexStr, toColorHexNum, isColorStr, colorNameToHex, colorToCSS, colorToLinearGradientCSS, mergeHexColorAlpha } from './lib/color';
export { createUUID, isAssetId, createAssetId } from './lib/uuid';
export { deepClone, sortDataAsserts, deepCloneElement } from './lib/data';
export { deepClone, sortDataAsserts, deepCloneElement, filterCompactData } from './lib/data';
export { istype } from './lib/istype';
export { loadImage, loadSVG, loadHTML } from './lib/load';
export { is } from './lib/is';
Expand Down
76 changes: 74 additions & 2 deletions packages/util/src/lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Data, ElementAssets, Elements, ElementType, Element } from '@idraw/types';
import { createAssetId, createUUID } from './uuid';
import type { Data, ElementAssets, Elements, ElementType, Element, LoadItemMap } from '@idraw/types';
import { createAssetId, createUUID, isAssetId } from './uuid';

export function deepClone<T = any>(target: T): T {
function _clone(t: T) {
Expand Down Expand Up @@ -82,7 +82,79 @@ export function sortDataAsserts(data: Data, opts?: { clone?: boolean }): Data {
const assetUUID = createAssetId(html);
if (!assets[assetUUID]) {
assets[assetUUID] = {
type: 'html',
value: html
};
}
(elem as Element<'html'>).detail.html = assetUUID;
} else if (elem.type === 'group' && Array.isArray((elem as Element<'group'>).detail.children)) {
const groupAssets = (elem as Element<'group'>).detail.assets || {};
Object.keys(groupAssets).forEach((assetId) => {
if (!assets[assetId]) {
assets[assetId] = groupAssets[assetId];
}
});
delete (elem as Element<'group'>).detail.assets;
_scanElements((elem as Element<'group'>).detail.children);
}
});
};

_scanElements(sortedData.elements);
sortedData.assets = assets;
return sortedData;
}

export function filterCompactData(data: Data, opts?: { loadItemMap?: LoadItemMap }) {
const assets: ElementAssets = data.assets || {};
const sortedData = deepClone(data);
const loadItemMap = opts?.loadItemMap || {};

const _scanElements = (elems: Elements) => {
elems.forEach((elem: Element<ElementType>) => {
if (elem.type === 'image' && (elem as Element<'image'>).detail.src) {
const src = (elem as Element<'image'>).detail.src;
if (isAssetId(src) && !assets[src] && loadItemMap[src] && typeof loadItemMap[src]?.source === 'string') {
assets[src] = {
type: 'image',
value: loadItemMap[src].source as string
};
} else {
const assetUUID = createAssetId(src);
if (!assets[assetUUID]) {
assets[assetUUID] = {
type: 'image',
value: src
};
}
(elem as Element<'image'>).detail.src = assetUUID;
}
} else if (elem.type === 'svg') {
const svg = (elem as Element<'svg'>).detail.svg;
const assetUUID = createAssetId(svg);
if (isAssetId(svg) && !assets[svg] && loadItemMap[svg] && typeof loadItemMap[svg]?.source === 'string') {
assets[svg] = {
type: 'svg',
value: loadItemMap[svg].source as string
};
} else if (!assets[assetUUID]) {
assets[assetUUID] = {
type: 'svg',
value: svg
};
}
(elem as Element<'svg'>).detail.svg = assetUUID;
} else if (elem.type === 'html') {
const html = (elem as Element<'html'>).detail.html;
const assetUUID = createAssetId(html);
if (isAssetId(html) && !assets[html] && loadItemMap[html] && typeof loadItemMap[html]?.source === 'string') {
assets[html] = {
type: 'html',
value: loadItemMap[html].source as string
};
} else if (!assets[assetUUID]) {
assets[assetUUID] = {
type: 'html',
value: html
};
}
Expand Down
7 changes: 5 additions & 2 deletions packages/util/src/lib/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ export function downloadImageFromCanvas(canvas: HTMLCanvasElement, opts: { fileN
downloadLink = null;
}

export function pickFile(opts: { success: (data: { file: File }) => void; error?: (err: ErrorEvent) => void }) {
const { success, error } = opts;
export function pickFile(opts: { accept?: string; success: (data: { file: File }) => void; error?: (err: Error | any) => void }) {
const { accept, success, error } = opts;
let input: HTMLInputElement | null = document.createElement('input') as HTMLInputElement;
input.type = 'file';
if (accept) {
input.accept = accept;
}
input.addEventListener('change', function () {
const file: File = (input as HTMLInputElement).files?.[0] as File;
success({
Expand Down
Loading