Skip to content

Commit

Permalink
fix: 优化设计器跳转
Browse files Browse the repository at this point in the history
  • Loading branch information
oustn committed Jul 31, 2024
1 parent 184b103 commit 5674add
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/contents/dev/actions/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export abstract class Action {
return;
}

async postWait() {}

private async getTarget() {
const config = await resolveConfig();
const rules = config.rules;
Expand All @@ -35,12 +37,13 @@ export abstract class Action {
};
}

protected dispatchOpen(url: string) {
protected dispatchOpen(url: string, options?: Record<string, unknown>) {
const spy = document.getElementById(SPY_ID);
if (!spy) return;
const event = new CustomEvent(OPEN_EVENT, {
detail: {
url,
options,
},
});
spy.dispatchEvent(event);
Expand Down
32 changes: 25 additions & 7 deletions src/contents/dev/actions/designer-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ class DesignerAction extends Action {
injectTarget = '.header-right .btns';

get renderActions(): RenderAction | RenderAction[] {
if (!this.target?.preview) return [];
return {
name: '预览',
execute: () => {
this.handleViewDev();
},
};
const result = []
if (this.target?.dev) {
result.push({
name: '开发',
execute: () => {
this.handleDev();
},
});
}
if (this.target?.preview) {
result.push({
name: '预览',
execute: () => {
this.handleViewDev();
},
})
}
return result;
}

isTarget(path: string): boolean {
Expand All @@ -40,6 +51,13 @@ class DesignerAction extends Action {
if (!this.metadata) return;
this.dispatchOpen(`${this.target?.preview ?? ''}${this.metadata.url}`);
}

private handleDev() {
if (!this.metadata) return;
this.dispatchOpen(`${this.target?.dev ?? ''}/mobile/`, {
withHash: true,
});
}
}

export const designerAction = new DesignerAction();
1 change: 1 addition & 0 deletions src/contents/dev/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function process(path: string, searchParams: string) {
actions.forEach(async action => {
if (action.isTarget(path, new URLSearchParams(searchParams))) {
await action.wait();
await action.postWait();
if (!action.renderActions || (Array.isArray(action.renderActions) && !action.renderActions.length)) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/contents/dev/actions/list-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class ListAction extends Action {
});
}

async postWait() {
const table = document.querySelector('.page-grid table')
if (table) {
table.classList.add('__inject_from_plugin_table')
}
}

get renderActions(): Array<RenderAction> | RenderAction {
const result = [];
if (this.target?.dev) {
Expand Down
4 changes: 2 additions & 2 deletions src/contents/dev/index.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.page-grid table > colgroup > col:nth-child(1) {
table.__inject_from_plugin_table > colgroup > col:nth-child(1) {
width: 550px !important;
}

.page-grid table > colgroup > col:nth-child(7) {
table.__inject_from_plugin_table > colgroup > col:nth-child(7) {
width: 300px !important;
}

Expand Down
8 changes: 7 additions & 1 deletion src/contents/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ if (spy) {
});

spy.addEventListener(OPEN_EVENT, (e: Event) => {
window.open((e as unknown as CustomEvent)?.detail?.url, '_blank');
const { url, options } = (e as unknown as CustomEvent)?.detail ?? {};
let target = url
if (!url) return
if (options && options.withHash) {
target = `${url}${window.location.hash}`
}
window.open(target, '_blank');
});
}

Expand Down

0 comments on commit 5674add

Please sign in to comment.