Skip to content

Commit

Permalink
fix(scirpt): 优化搜索结果的显示,并且添加快捷百度一下按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 23, 2023
1 parent b263146 commit e074a95
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/core/answer-wrapper/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export interface Result {

/** 题库查询信息 */
export interface SearchInformation {
url: string;
results: Result[];
name: string;
url?: string;
/** 主页 */
homepage?: string;
/** 题目答案 */
results: Result[];
/** 请求响应内容 */
response: any;
response?: any;
/** 请求发起内容 */
data: any;
data?: any;
/** 错误数据 */
error?: Error;
error?: string;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/elements/search.infos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SimplifyWorkResult } from '../core/worker';
import { splitAnswer } from '../core/worker/utils';
import { $creator } from '../utils';
import { $ } from '../utils/common';
import { $creator } from '../utils/creator';
import { el } from '../utils/dom';
import { IElement } from './interface';

Expand All @@ -22,7 +22,7 @@ export class SearchInfosElement extends IElement {
const question = transformImgLink(this.question || '无');

this.append(
el('div', [el('span', { innerHTML: question }), $creator.copy('复制', question)], (div) => {
el('div', [el('span', { innerHTML: question }), $creator.createQuestionTitleExtra(this.question)], (div) => {
div.style.padding = '4px';
}),
el('hr')
Expand All @@ -31,7 +31,7 @@ export class SearchInfosElement extends IElement {
this.append(
...this.infos.map((info) => {
return el('details', { open: true }, [
el('summary', [el('a', { href: info.homepage, innerText: info.name })]),
el('summary', [el('a', { href: info.homepage, innerText: info.name, target: '_blank' })]),
...(info.error
? /** 显示错误信息 */
[el('span', { className: 'error' }, [info.error || '网络错误或者未知错误'])]
Expand All @@ -43,15 +43,11 @@ export class SearchInfosElement extends IElement {

return el('div', { className: 'search-result' }, [
/** 题目 */
el('div', { className: 'question' }, [
el('span', { innerHTML: title }),
$creator.copy('复制', title)
]),
el('div', { className: 'question' }, [el('span', { innerHTML: title })]),
/** 答案 */
el('div', { className: 'answer' }, [
el('span', '答案:'),
...splitAnswer(answer).map((a) => el('code', { innerHTML: a })),
$creator.copy('复制', splitAnswer(answer).join(' '))
...splitAnswer(answer).map((a) => el('code', { innerHTML: a }))
])
]);
})
Expand Down
38 changes: 38 additions & 0 deletions packages/core/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,43 @@ export const $ = {
/** 是否处于顶级 window ,而不是子 iframe */
isInTopWindow() {
return self === top;
},
/**
* 创建弹出窗口
* @param url 地址
* @param winName 窗口名
* @param w 宽
* @param h 高
* @param scroll 滚动条
*/
createCenteredPopupWindow(
url: string,
winName: string,
opts: {
width: number;
height: number;
scrollbars: boolean;
resizable: boolean;
}
) {
const { width, height, scrollbars, resizable } = opts;
const LeftPosition = screen.width ? (screen.width - width) / 2 : 0;
const TopPosition = screen.height ? (screen.height - height) / 2 : 0;

const settings =
'height=' +
height +
',width=' +
width +
',top=' +
TopPosition +
',left=' +
LeftPosition +
',scrollbars=' +
(scrollbars ? 'yes' : 'no') +
',resizable=' +
(resizable ? 'yes' : 'no');

return window.open(url, winName, settings);
}
};
70 changes: 68 additions & 2 deletions packages/core/src/utils/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export interface PreventTextOptions {
onprevent?: (span: HTMLSpanElement) => void;
}

let popupWin: Window | null;
window.addEventListener('beforeunload', () => {
popupWin?.close();
});

/**
* 元素创建器
*/
Expand All @@ -33,7 +38,14 @@ export const $creator = {
notes(lines: (string | HTMLElement | (string | HTMLElement)[])[], tag: 'ul' | 'ol' = 'ul') {
return el(
tag,
lines.map((line) => el('li', Array.isArray(line) ? line.map((node) => el('div', [node])) : [line]))
lines.map((line) =>
el(
'li',
Array.isArray(line)
? line.map((node) => (typeof node === 'string' ? el('div', { innerHTML: node }) : node))
: [typeof line === 'string' ? el('div', { innerHTML: line }) : line]
)
)
);
},
/**
Expand Down Expand Up @@ -139,7 +151,17 @@ export const $creator = {

return scriptPanel;
},
/** 创建设置区域 */
/** 创建独立的设置区域 */
configsArea(configElements: Record<string, ConfigElement<any>>) {
/** 创建设置板块 */
const configsContainer: HTMLDivElement = el('div', { className: 'configs card' });
/** 设置区域主体 */
const configsBody: HTMLDivElement = el('div', { className: 'configs-body' });
configsBody.append(...Object.entries(configElements).map(([key, el]) => el));
configsContainer.append(configsBody);
return configsContainer;
},
/** 创建设置元素 */
configs<T extends Record<string, Config<any>>>(
namespace: string | undefined,
configs: T,
Expand Down Expand Up @@ -210,5 +232,49 @@ export const $creator = {
}, delay * 1000);

return span;
},
/**
* 创建关于问题题目的拓展功能按钮,包括复制和百度一下
* @param question 问题
*/
createQuestionTitleExtra(question: string) {
const space = $creator.space(
[
$creator.copy('复制', question),
el('span', { className: 'question-title-extra-btn', innerText: '🌏百度一下' }, (btn) => {
btn.onclick = () => {
popupWin?.close();
popupWin = $.createCenteredPopupWindow(`https://www.baidu.com/s?wd=${question}`, '百度搜索', {
width: 800,
height: 600,
resizable: true,
scrollbars: true
});
};
})
],
{ x: 4 }
);
space.style.marginTop = '6px';
space.style.textAlign = 'right';
return space;
},
/**
* 将所有子元素隔开
* x: 默认 12
* y: 默认 0
*/
space(children: HTMLElement[], options?: { x?: number; y?: number }) {
return el('div', { className: 'space' }, (div) => {
for (let index = 0; index < children.length; index++) {
const child = el('span', { className: 'space-item' }, [children[index]]);
child.style.display = 'inline-block';
if (index > 0) {
child.style.marginLeft = (options?.x ?? 12) + 'px';
child.style.marginTop = (options?.y ?? 0) + 'px';
}
div.append(child);
}
});
}
};
2 changes: 1 addition & 1 deletion packages/scripts/src/utils/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function simplifyWorkResult(
finish: wr.result?.finish,
searchInfos:
wr.ctx?.searchInfos.map((sr) => ({
error: sr.error ? sr.error?.message || String(sr.error) : undefined,
error: sr.error,
name: sr.name,
homepage: sr.homepage,
results: sr.results.map((ans) => [ans.question, ans.answer])
Expand Down

0 comments on commit e074a95

Please sign in to comment.