|
|
|
|
@ -21,11 +21,16 @@
|
|
|
|
|
page: null,
|
|
|
|
|
shared: null,
|
|
|
|
|
},
|
|
|
|
|
reads: {
|
|
|
|
|
page: { controller: null, serial: 0 },
|
|
|
|
|
shared: { controller: null, serial: 0 },
|
|
|
|
|
},
|
|
|
|
|
draftBuffer: new Map(),
|
|
|
|
|
dirtyKeys: new Set(),
|
|
|
|
|
locale: bootstrap.runtime_locale === 'zh-CN'
|
|
|
|
|
? '' : String(bootstrap.runtime_locale || ''),
|
|
|
|
|
query: '',
|
|
|
|
|
searchTimer: null,
|
|
|
|
|
writeLocked: false,
|
|
|
|
|
previousFocus: null,
|
|
|
|
|
};
|
|
|
|
|
@ -119,11 +124,18 @@
|
|
|
|
|
var list = element('div', 'user-copy-page-editor-list');
|
|
|
|
|
list.id = 'user-copy-page-editor-panel';
|
|
|
|
|
list.setAttribute('role', 'tabpanel');
|
|
|
|
|
list.setAttribute('aria-busy', 'false');
|
|
|
|
|
var catalogFooter = element('div', 'user-copy-page-editor-catalog-footer');
|
|
|
|
|
var catalogStatus = element('p', '', '尚未读取译文目录');
|
|
|
|
|
var loadMore = element('button', '', '加载更多');
|
|
|
|
|
loadMore.type = 'button';
|
|
|
|
|
loadMore.hidden = true;
|
|
|
|
|
catalogFooter.append(catalogStatus, loadMore);
|
|
|
|
|
var footer = element('footer', 'user-copy-page-editor-footer');
|
|
|
|
|
var adminLink = element('a', '', '前往后台翻译中心发布');
|
|
|
|
|
adminLink.href = '/admin/user-copy';
|
|
|
|
|
footer.append(adminLink);
|
|
|
|
|
drawer.append(header, tabs, controls, feedback, list, footer);
|
|
|
|
|
drawer.append(header, tabs, controls, feedback, list, catalogFooter, footer);
|
|
|
|
|
document.body.append(launch, backdrop, drawer);
|
|
|
|
|
|
|
|
|
|
function activeWorkspace() {
|
|
|
|
|
@ -232,19 +244,43 @@
|
|
|
|
|
var workspace = activeWorkspace();
|
|
|
|
|
var entries = workspace && Array.isArray(workspace.entries)
|
|
|
|
|
? workspace.entries : [];
|
|
|
|
|
var query = state.query.trim().toLowerCase();
|
|
|
|
|
return entries.filter(function (entry) {
|
|
|
|
|
if (!state.bindings.has(String(entry.key || ''))) return false;
|
|
|
|
|
if (state.activeTab === 'page' && entry.editor_surface !== 'page') return false;
|
|
|
|
|
if (state.activeTab === 'shared'
|
|
|
|
|
&& (entry.editor_surface !== 'shared'
|
|
|
|
|
|| !sharedGroups.has(String(entry.editor_group || '')))) return false;
|
|
|
|
|
if (!query) return true;
|
|
|
|
|
return String(entry.key || '').toLowerCase().indexOf(query) !== -1
|
|
|
|
|
|| String(entry.source || '').toLowerCase().indexOf(query) !== -1;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function paginationFor(workspace) {
|
|
|
|
|
var entries = workspace && Array.isArray(workspace.entries)
|
|
|
|
|
? workspace.entries : [];
|
|
|
|
|
var pagination = workspace && workspace.pagination
|
|
|
|
|
&& typeof workspace.pagination === 'object'
|
|
|
|
|
? workspace.pagination : {};
|
|
|
|
|
return {
|
|
|
|
|
page: Math.max(1, Number(pagination.page) || 1),
|
|
|
|
|
perPage: Math.max(1, Number(pagination.per_page) || 100),
|
|
|
|
|
total: Math.max(0, Number(pagination.total) || entries.length),
|
|
|
|
|
hasMore: pagination.has_more === true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderPagination() {
|
|
|
|
|
var workspace = activeWorkspace();
|
|
|
|
|
var entries = workspace && Array.isArray(workspace.entries)
|
|
|
|
|
? workspace.entries : [];
|
|
|
|
|
var pagination = paginationFor(workspace);
|
|
|
|
|
var visible = visibleEntries().length;
|
|
|
|
|
catalogStatus.textContent = '当前页面显示 ' + String(visible)
|
|
|
|
|
+ ' 项 · 已读取目录 ' + String(entries.length) + ' / '
|
|
|
|
|
+ String(pagination.total) + ' 项';
|
|
|
|
|
loadMore.hidden = !pagination.hasMore;
|
|
|
|
|
loadMore.disabled = list.getAttribute('aria-busy') === 'true';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderLocales() {
|
|
|
|
|
localeSelect.replaceChildren();
|
|
|
|
|
var workspace = activeWorkspace();
|
|
|
|
|
@ -321,14 +357,36 @@
|
|
|
|
|
? '当前页面没有已绑定的公共框架文案。'
|
|
|
|
|
: '当前页面没有可编辑的正文文案。'));
|
|
|
|
|
}
|
|
|
|
|
renderPagination();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mergeEntries(current, incoming) {
|
|
|
|
|
var merged = new Map();
|
|
|
|
|
(current || []).forEach(function (entry) {
|
|
|
|
|
merged.set(String(entry.key || ''), entry);
|
|
|
|
|
});
|
|
|
|
|
(incoming || []).forEach(function (entry) {
|
|
|
|
|
merged.set(String(entry.key || ''), entry);
|
|
|
|
|
});
|
|
|
|
|
return Array.from(merged.values()).sort(function (left, right) {
|
|
|
|
|
return String(left.key || '').localeCompare(String(right.key || ''));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyWorkspace(channel, workspace) {
|
|
|
|
|
function applyWorkspace(channel, workspace, append, query) {
|
|
|
|
|
if (!workspace || workspace.schema !== 1
|
|
|
|
|
|| !workspace.authoring || workspace.authoring.channel !== channel
|
|
|
|
|
|| !workspace.selected || workspace.selected.page !== bootstrap.page) {
|
|
|
|
|
throw new Error('页面译文目录响应无效。');
|
|
|
|
|
}
|
|
|
|
|
var current = state.workspaces[channel];
|
|
|
|
|
if (append && current
|
|
|
|
|
&& String(current.selected && current.selected.locale || '')
|
|
|
|
|
=== String(workspace.selected.locale || '')
|
|
|
|
|
&& String(current.catalog_query || '') === String(query || '')) {
|
|
|
|
|
workspace.entries = mergeEntries(current.entries, workspace.entries);
|
|
|
|
|
}
|
|
|
|
|
workspace.catalog_query = String(query || '');
|
|
|
|
|
state.workspaces[channel] = workspace;
|
|
|
|
|
if (channel !== state.activeTab) return;
|
|
|
|
|
state.locale = String(workspace.selected.locale || '');
|
|
|
|
|
@ -336,36 +394,105 @@
|
|
|
|
|
renderEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadWorkspace(channel, requestedLocale) {
|
|
|
|
|
function isCancelledRead(error) {
|
|
|
|
|
return Boolean(error && (error.name === 'AbortError'
|
|
|
|
|
|| error.code === 'cancelled'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function abortRead(channel) {
|
|
|
|
|
var read = state.reads[channel];
|
|
|
|
|
if (!read) return;
|
|
|
|
|
read.serial += 1;
|
|
|
|
|
if (read.controller) read.controller.abort('superseded');
|
|
|
|
|
read.controller = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function abortAllReads() {
|
|
|
|
|
abortRead('page');
|
|
|
|
|
abortRead('shared');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearSearchTimer() {
|
|
|
|
|
if (state.searchTimer === null) return;
|
|
|
|
|
clearTimeout(state.searchTimer);
|
|
|
|
|
state.searchTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadWorkspace(channel, requestedLocale, options) {
|
|
|
|
|
if (channel !== 'page' && channel !== 'shared') {
|
|
|
|
|
throw new Error('页面译文编辑范围无效。');
|
|
|
|
|
}
|
|
|
|
|
options = options && typeof options === 'object' ? options : {};
|
|
|
|
|
var append = options.append === true;
|
|
|
|
|
var current = state.workspaces[channel];
|
|
|
|
|
var currentPagination = paginationFor(current);
|
|
|
|
|
var catalogPage = append ? currentPagination.page + 1 : 1;
|
|
|
|
|
var requestedQuery = String(state.query || '').trim();
|
|
|
|
|
collectBindings();
|
|
|
|
|
var query = new URLSearchParams();
|
|
|
|
|
query.set('channel', channel);
|
|
|
|
|
query.set('page', bootstrap.page);
|
|
|
|
|
query.set('catalog_page', String(catalogPage));
|
|
|
|
|
query.set('per_page', '100');
|
|
|
|
|
if (requestedQuery) query.set('q', requestedQuery);
|
|
|
|
|
if (requestedLocale && requestedLocale !== 'zh-CN') {
|
|
|
|
|
query.set('locale', requestedLocale);
|
|
|
|
|
}
|
|
|
|
|
abortRead(channel);
|
|
|
|
|
var read = state.reads[channel];
|
|
|
|
|
var serial = read.serial;
|
|
|
|
|
var controller = new AbortController();
|
|
|
|
|
read.controller = controller;
|
|
|
|
|
if (channel === state.activeTab) {
|
|
|
|
|
inform('正在读取' + tabLabel(channel) + '译文…', 'info');
|
|
|
|
|
list.setAttribute('aria-busy', 'true');
|
|
|
|
|
loadMore.disabled = true;
|
|
|
|
|
inform((append ? '正在加载更多' : '正在读取')
|
|
|
|
|
+ tabLabel(channel) + '译文…', 'info');
|
|
|
|
|
}
|
|
|
|
|
var result = await transport.read('/admin/user-copy/entries?' + query.toString());
|
|
|
|
|
applyWorkspace(channel, result && result.workspace);
|
|
|
|
|
if (channel === state.activeTab) {
|
|
|
|
|
var workspace = activeWorkspace();
|
|
|
|
|
inform(workspace.write_ready === true
|
|
|
|
|
? tabLabel(channel) + '译文已加载。' : workspace.write_block_reason, 'info');
|
|
|
|
|
try {
|
|
|
|
|
var result = await transport.read(
|
|
|
|
|
'/admin/user-copy/entries?' + query.toString(),
|
|
|
|
|
{ signal: controller.signal }
|
|
|
|
|
);
|
|
|
|
|
if (read.serial !== serial || read.controller !== controller
|
|
|
|
|
|| requestedQuery !== String(state.query || '').trim()) return;
|
|
|
|
|
var workspace = result && result.workspace;
|
|
|
|
|
if (requestedLocale && requestedLocale !== 'zh-CN'
|
|
|
|
|
&& String(workspace && workspace.selected
|
|
|
|
|
&& workspace.selected.locale || '') !== requestedLocale) {
|
|
|
|
|
throw new Error('页面译文目录语言不匹配。');
|
|
|
|
|
}
|
|
|
|
|
applyWorkspace(channel, workspace, append, requestedQuery);
|
|
|
|
|
if (channel === state.activeTab) {
|
|
|
|
|
workspace = activeWorkspace();
|
|
|
|
|
inform(workspace.write_ready === true
|
|
|
|
|
? tabLabel(channel) + '译文已加载。'
|
|
|
|
|
: workspace.write_block_reason, 'info');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (isCancelledRead(error) || read.serial !== serial
|
|
|
|
|
|| requestedQuery !== String(state.query || '').trim()) return;
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
if (read.serial === serial && read.controller === controller) {
|
|
|
|
|
read.controller = null;
|
|
|
|
|
if (channel === state.activeTab) {
|
|
|
|
|
list.setAttribute('aria-busy', 'false');
|
|
|
|
|
renderPagination();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function switchTab(tab) {
|
|
|
|
|
if (tab !== 'page' && tab !== 'shared') return;
|
|
|
|
|
clearSearchTimer();
|
|
|
|
|
state.activeTab = tab;
|
|
|
|
|
updateTabUi();
|
|
|
|
|
var workspace = activeWorkspace();
|
|
|
|
|
if (workspace
|
|
|
|
|
&& String(workspace.selected && workspace.selected.locale || '') === state.locale) {
|
|
|
|
|
&& String(workspace.selected && workspace.selected.locale || '') === state.locale
|
|
|
|
|
&& String(workspace.catalog_query || '') === String(state.query || '').trim()) {
|
|
|
|
|
renderLocales();
|
|
|
|
|
renderEntries();
|
|
|
|
|
inform(tabLabel(tab) + '译文已加载。', 'info');
|
|
|
|
|
@ -465,6 +592,8 @@
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
discardAllDrafts();
|
|
|
|
|
abortAllReads();
|
|
|
|
|
clearSearchTimer();
|
|
|
|
|
drawer.hidden = true;
|
|
|
|
|
backdrop.hidden = true;
|
|
|
|
|
document.body.classList.remove('user-copy-page-editor-open');
|
|
|
|
|
@ -499,6 +628,8 @@
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
discardLocaleDrafts(previousLocale);
|
|
|
|
|
abortAllReads();
|
|
|
|
|
clearSearchTimer();
|
|
|
|
|
state.locale = nextLocale;
|
|
|
|
|
state.workspaces.page = null;
|
|
|
|
|
state.workspaces.shared = null;
|
|
|
|
|
@ -508,7 +639,27 @@
|
|
|
|
|
});
|
|
|
|
|
search.addEventListener('input', function () {
|
|
|
|
|
state.query = search.value;
|
|
|
|
|
renderEntries();
|
|
|
|
|
abortRead(state.activeTab);
|
|
|
|
|
if (state.searchTimer !== null) clearTimeout(state.searchTimer);
|
|
|
|
|
state.searchTimer = setTimeout(function () {
|
|
|
|
|
state.searchTimer = null;
|
|
|
|
|
state.workspaces[state.activeTab] = null;
|
|
|
|
|
loadWorkspace(state.activeTab, state.locale).catch(function (error) {
|
|
|
|
|
if (!isCancelledRead(error)) {
|
|
|
|
|
inform(error && error.message
|
|
|
|
|
? error.message : '页面译文搜索失败。', 'error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 250);
|
|
|
|
|
});
|
|
|
|
|
loadMore.addEventListener('click', function () {
|
|
|
|
|
loadWorkspace(state.activeTab, state.locale, { append: true })
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
if (!isCancelledRead(error)) {
|
|
|
|
|
inform(error && error.message
|
|
|
|
|
? error.message : '更多页面译文读取失败。', 'error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
document.addEventListener('keydown', function (event) {
|
|
|
|
|
if (drawer.hidden) return;
|
|
|
|
|
|