Skip to content

Commit

Permalink
feat(script): 增加搜索结果与题目同步显示功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed May 9, 2023
1 parent c877ed6 commit f793d43
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
52 changes: 51 additions & 1 deletion packages/scripts/src/projects/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ const gotoHome = () => {
return btn;
};

const state = {
workResult: {
/**
* 题目位置同步处理器
*/
questionPositionSyncHandler: {
cx: (index: number) => {
const el = document.querySelectorAll<HTMLElement>('[id*="sigleQuestionDiv"], .questionLi')?.item(index);
if (el) {
window.scrollTo({
top: el.getBoundingClientRect().top + window.pageYOffset - 50,
behavior: 'smooth'
});
}
},
'zhs-gxk': (index: number) => {
document.querySelectorAll<HTMLElement>('.answerCard_list ul li').item(index)?.click();
},
'zhs-xnk': (index: number) => {
document.querySelectorAll<HTMLElement>('.jobclassallnumber-div li[questionid]').item(index)?.click();
},
icve: (index: number) => {
document.querySelectorAll<HTMLElement>(`.sheet_nums [id*="sheetSeq"]`).item(index)?.click();
},
zjy: (index: number) => {
document
.querySelector<HTMLElement>(`.e-q-body[data-num="${index + 1}"]`)
?.scrollIntoView({ behavior: 'smooth' });
}
}
}
};

/**
* 题库缓存类型
*/
Expand Down Expand Up @@ -380,6 +413,9 @@ export const CommonProject = Project.create({
},
currentResultIndex: {
defaultValue: 0
},
questionPositionSyncHandlerType: {
defaultValue: undefined as keyof typeof state.workResult.questionPositionSyncHandler | undefined
}
},
methods() {
Expand Down Expand Up @@ -415,7 +451,9 @@ export const CommonProject = Project.create({
/**
* 刷新搜索结果状态,清空搜索结果,置顶搜索结果面板
*/
init() {
init(opts?: { questionPositionSyncHandlerType?: keyof typeof state.workResult.questionPositionSyncHandler }) {
CommonProject.scripts.workResults.cfg.questionPositionSyncHandlerType =
opts?.questionPositionSyncHandlerType;
// 刷新搜索结果状态
CommonProject.scripts.workResults.methods.refreshState();
// 清空搜索结果
Expand Down Expand Up @@ -486,6 +524,12 @@ export const CommonProject = Project.create({
this.cfg.currentResultIndex = index;
// 重新渲染结果列表
resultContainer.replaceChildren(createResult(result));
// 触发页面题目元素同步器
if (this.cfg.questionPositionSyncHandlerType) {
state.workResult.questionPositionSyncHandler[this.cfg.questionPositionSyncHandlerType]?.(
index
);
}
};
});
});
Expand Down Expand Up @@ -567,6 +611,12 @@ export const CommonProject = Project.create({
this.cfg.currentResultIndex = index;
// 重新渲染结果列表
resultContainer.replaceChildren(createResult(result));
// 触发页面题目元素同步器
if (this.cfg.questionPositionSyncHandlerType) {
state.workResult.questionPositionSyncHandler[this.cfg.questionPositionSyncHandlerType]?.(
index
);
}
};
}
);
Expand Down
4 changes: 3 additions & 1 deletion packages/scripts/src/projects/cx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ export function workOrExam(
) {
$message('info', { content: `开始${type === 'work' ? '作业' : '考试'}` });

CommonProject.scripts.workResults.methods.init();
CommonProject.scripts.workResults.methods.init({
questionPositionSyncHandlerType: 'cx'
});

// 处理作业和考试题目的方法
const workOrExamQuestionTitleTransform = (titles: (HTMLElement | undefined)[]) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/src/projects/icve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ function work({ answererWrappers, period, thread }: CommonWorkOptions) {
}

$message('info', { content: '作业/考试完成,请自行检查后保存或提交。', duration: 0 });
// 搜索完成后才会同步答案与题目的显示,防止题目错乱
CommonProject.scripts.workResults.cfg.questionPositionSyncHandlerType = 'icve';
})();

return worker;
Expand Down
6 changes: 5 additions & 1 deletion packages/scripts/src/projects/zhs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,9 @@ function gxkWorkOrExam(
{ answererWrappers, period, upload, thread, stopSecondWhenFinish, redundanceWordsText }: CommonWorkOptions
) {
$message('info', { content: `开始${type === 'work' ? '作业' : '考试'}` });
CommonProject.scripts.workResults.methods.init();
CommonProject.scripts.workResults.methods.init({
questionPositionSyncHandlerType: 'zhs-gxk'
});

const titleTransform = (titles: (HTMLElement | undefined)[]) => {
return removeRedundantWords(
Expand Down Expand Up @@ -1185,6 +1187,8 @@ function xnkWork({ answererWrappers, period, thread }: CommonWorkOptions) {
}

$message('info', { content: '作业/考试完成,请自行检查后保存或提交。', duration: 0 });

CommonProject.scripts.workResults.cfg.questionPositionSyncHandlerType = 'zhs-xnk';
})();

return worker;
Expand Down

0 comments on commit f793d43

Please sign in to comment.