|
|
(function () {
|
|
|
'use strict';
|
|
|
|
|
|
var bootstrapNode = document.getElementById('user-copy-editor-bootstrap');
|
|
|
if (!bootstrapNode || !window.VenixAdminRequestRuntime) return;
|
|
|
|
|
|
var bootstrap;
|
|
|
try { bootstrap = JSON.parse(bootstrapNode.textContent || '{}'); }
|
|
|
catch (error) { bootstrap = null; }
|
|
|
if (!bootstrap || bootstrap.schema !== 1
|
|
|
|| !/^[a-z0-9][a-z0-9._-]{1,190}$/.test(String(bootstrap.page || ''))
|
|
|
|| !/^[a-f0-9]{64}$/.test(String(bootstrap.csrf_token || ''))) return;
|
|
|
|
|
|
var transport = window.VenixAdminRequestRuntime.create({
|
|
|
csrfToken: function () { return bootstrap.csrf_token; },
|
|
|
});
|
|
|
var state = {
|
|
|
bindings: new Map(),
|
|
|
activeTab: 'page',
|
|
|
workspaces: {
|
|
|
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,
|
|
|
};
|
|
|
|
|
|
function sharedGroupsForPage(page) {
|
|
|
var groups = new Set(['common.shared']);
|
|
|
if (/^(cloud|user)\./.test(page)) groups.add('shell.user');
|
|
|
if (/^(auth|legal|public)\./.test(page)) groups.add('shell.public');
|
|
|
return groups;
|
|
|
}
|
|
|
|
|
|
var sharedGroups = sharedGroupsForPage(String(bootstrap.page));
|
|
|
|
|
|
function element(tag, className, text) {
|
|
|
var node = document.createElement(tag);
|
|
|
if (className) node.className = className;
|
|
|
if (text !== undefined) node.textContent = text;
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
function collectBindings() {
|
|
|
state.bindings = new Map();
|
|
|
document.querySelectorAll('[data-user-copy-key]').forEach(function (node) {
|
|
|
var key = String(node.getAttribute('data-user-copy-key') || '');
|
|
|
if (!/^[a-z0-9][a-z0-9._-]{1,190}$/.test(key)) return;
|
|
|
if (!state.bindings.has(key)) state.bindings.set(key, []);
|
|
|
state.bindings.get(key).push(node);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
collectBindings();
|
|
|
|
|
|
var launch = element('button', 'user-copy-page-editor-launch', '编辑页面译文');
|
|
|
launch.type = 'button';
|
|
|
launch.setAttribute('aria-haspopup', 'dialog');
|
|
|
launch.setAttribute('aria-controls', 'user-copy-page-editor');
|
|
|
|
|
|
var backdrop = element('div', 'user-copy-page-editor-backdrop');
|
|
|
backdrop.hidden = true;
|
|
|
var drawer = element('aside', 'user-copy-page-editor');
|
|
|
drawer.id = 'user-copy-page-editor';
|
|
|
drawer.hidden = true;
|
|
|
drawer.setAttribute('role', 'dialog');
|
|
|
drawer.setAttribute('aria-modal', 'true');
|
|
|
drawer.setAttribute('aria-labelledby', 'user-copy-page-editor-title');
|
|
|
|
|
|
var header = element('header', 'user-copy-page-editor-header');
|
|
|
var heading = element('div');
|
|
|
var title = element('h2', '', '编辑页面译文');
|
|
|
title.id = 'user-copy-page-editor-title';
|
|
|
heading.append(title, element('p', '', '这里只保存草稿,统一发布仍在后台翻译中心完成。'));
|
|
|
var close = element('button', 'user-copy-page-editor-close', '×');
|
|
|
close.type = 'button';
|
|
|
close.setAttribute('aria-label', '关闭页面译文编辑器');
|
|
|
header.append(heading, close);
|
|
|
|
|
|
var tabs = element('div', 'user-copy-page-editor-tabs');
|
|
|
tabs.setAttribute('role', 'tablist');
|
|
|
tabs.setAttribute('aria-label', '译文编辑范围');
|
|
|
var pageTab = element('button', '', '当前页面正文');
|
|
|
pageTab.id = 'user-copy-page-editor-tab-page';
|
|
|
pageTab.type = 'button';
|
|
|
pageTab.dataset.userCopyEditorTab = 'page';
|
|
|
pageTab.setAttribute('role', 'tab');
|
|
|
pageTab.setAttribute('aria-controls', 'user-copy-page-editor-panel');
|
|
|
var sharedTab = element('button', '', '公共框架');
|
|
|
sharedTab.id = 'user-copy-page-editor-tab-shared';
|
|
|
sharedTab.type = 'button';
|
|
|
sharedTab.dataset.userCopyEditorTab = 'shared';
|
|
|
sharedTab.setAttribute('role', 'tab');
|
|
|
sharedTab.setAttribute('aria-controls', 'user-copy-page-editor-panel');
|
|
|
tabs.append(pageTab, sharedTab);
|
|
|
|
|
|
var controls = element('div', 'user-copy-page-editor-controls');
|
|
|
var localeLabel = element('label', '', '目标语言');
|
|
|
var localeSelect = element('select');
|
|
|
localeSelect.id = 'user-copy-page-editor-locale';
|
|
|
localeLabel.htmlFor = localeSelect.id;
|
|
|
var searchLabel = element('label', '', '搜索当前页面文案');
|
|
|
var search = element('input');
|
|
|
search.id = 'user-copy-page-editor-search';
|
|
|
search.type = 'search';
|
|
|
search.placeholder = '键名或中文源文案';
|
|
|
searchLabel.htmlFor = search.id;
|
|
|
controls.append(localeLabel, localeSelect, searchLabel, search);
|
|
|
|
|
|
var feedback = element('p', 'user-copy-page-editor-feedback');
|
|
|
feedback.setAttribute('role', 'status');
|
|
|
feedback.setAttribute('aria-live', 'polite');
|
|
|
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, catalogFooter, footer);
|
|
|
document.body.append(launch, backdrop, drawer);
|
|
|
|
|
|
function activeWorkspace() {
|
|
|
return state.workspaces[state.activeTab];
|
|
|
}
|
|
|
|
|
|
function tabLabel(tab) {
|
|
|
return tab === 'shared' ? '公共框架' : '当前页面正文';
|
|
|
}
|
|
|
|
|
|
function updateTabUi() {
|
|
|
[pageTab, sharedTab].forEach(function (button) {
|
|
|
var selected = button.dataset.userCopyEditorTab === state.activeTab;
|
|
|
button.setAttribute('aria-selected', selected ? 'true' : 'false');
|
|
|
button.tabIndex = selected ? 0 : -1;
|
|
|
});
|
|
|
var activeTab = state.activeTab === 'shared' ? sharedTab : pageTab;
|
|
|
list.setAttribute('aria-labelledby', activeTab.id);
|
|
|
searchLabel.textContent = state.activeTab === 'shared'
|
|
|
? '搜索公共框架文案' : '搜索当前页面正文';
|
|
|
}
|
|
|
|
|
|
function inform(message, kind) {
|
|
|
feedback.textContent = String(message || '');
|
|
|
feedback.dataset.kind = kind || '';
|
|
|
}
|
|
|
|
|
|
function removeHighlights() {
|
|
|
document.querySelectorAll('.is-user-copy-page-highlight').forEach(function (node) {
|
|
|
node.classList.remove('is-user-copy-page-highlight');
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function locate(key) {
|
|
|
removeHighlights();
|
|
|
var nodes = state.bindings.get(key) || [];
|
|
|
nodes.forEach(function (node) { node.classList.add('is-user-copy-page-highlight'); });
|
|
|
if (nodes[0] && typeof nodes[0].scrollIntoView === 'function') {
|
|
|
nodes[0].scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function lockWrites(message) {
|
|
|
state.writeLocked = true;
|
|
|
drawer.querySelectorAll('[data-user-copy-save]').forEach(function (button) {
|
|
|
button.disabled = true;
|
|
|
});
|
|
|
inform(message, 'error');
|
|
|
}
|
|
|
|
|
|
function textFor(entry) {
|
|
|
if (entry.draft && typeof entry.draft.text === 'string') return entry.draft.text;
|
|
|
if (entry.published && typeof entry.published.text === 'string') return entry.published.text;
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
function bufferKey(locale, copyKey) {
|
|
|
return String(locale || '') + '\u0000' + String(copyKey || '');
|
|
|
}
|
|
|
|
|
|
function draftFor(entry, locale) {
|
|
|
var key = bufferKey(locale, entry.key);
|
|
|
if (state.draftBuffer.has(key)) return state.draftBuffer.get(key);
|
|
|
return { text: textFor(entry) };
|
|
|
}
|
|
|
|
|
|
function rememberDraft(entry, locale, text) {
|
|
|
var key = bufferKey(locale, entry.key);
|
|
|
if (text === textFor(entry)) {
|
|
|
state.draftBuffer.delete(key);
|
|
|
state.dirtyKeys.delete(key);
|
|
|
return;
|
|
|
}
|
|
|
state.draftBuffer.set(key, { text: text });
|
|
|
state.dirtyKeys.add(key);
|
|
|
}
|
|
|
|
|
|
function clearBufferedDraft(locale, copyKey) {
|
|
|
var key = bufferKey(locale, copyKey);
|
|
|
state.draftBuffer.delete(key);
|
|
|
state.dirtyKeys.delete(key);
|
|
|
}
|
|
|
|
|
|
function hasDirtyLocale(locale) {
|
|
|
var prefix = String(locale || '') + '\u0000';
|
|
|
return Array.from(state.dirtyKeys).some(function (key) {
|
|
|
return key.indexOf(prefix) === 0;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function discardLocaleDrafts(locale) {
|
|
|
var prefix = String(locale || '') + '\u0000';
|
|
|
Array.from(state.dirtyKeys).forEach(function (key) {
|
|
|
if (key.indexOf(prefix) !== 0) return;
|
|
|
state.dirtyKeys.delete(key);
|
|
|
state.draftBuffer.delete(key);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function discardAllDrafts() {
|
|
|
state.dirtyKeys.clear();
|
|
|
state.draftBuffer.clear();
|
|
|
}
|
|
|
|
|
|
function visibleEntries() {
|
|
|
var workspace = activeWorkspace();
|
|
|
var entries = workspace && Array.isArray(workspace.entries)
|
|
|
? workspace.entries : [];
|
|
|
return entries.filter(function (entry) {
|
|
|
if (state.activeTab === 'page') {
|
|
|
return entry.editor_surface === 'page';
|
|
|
}
|
|
|
return entry.editor_surface === 'shared'
|
|
|
&& sharedGroups.has(String(entry.editor_group || ''))
|
|
|
&& state.bindings.has(String(entry.key || ''));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
var locales = workspace && Array.isArray(workspace.locales)
|
|
|
? workspace.locales : [];
|
|
|
locales.forEach(function (locale) {
|
|
|
if (!locale || locale.id === 'zh-CN') return;
|
|
|
var option = element('option', '', locale.label || locale.id);
|
|
|
option.value = String(locale.id || '');
|
|
|
option.selected = option.value === state.locale;
|
|
|
option.disabled = locale.enabled !== true;
|
|
|
localeSelect.append(option);
|
|
|
});
|
|
|
localeSelect.disabled = state.writeLocked || localeSelect.options.length === 0;
|
|
|
}
|
|
|
|
|
|
function renderEntry(entry) {
|
|
|
var workspace = activeWorkspace();
|
|
|
var buffered = draftFor(entry, state.locale);
|
|
|
var card = element('article', 'user-copy-page-editor-entry');
|
|
|
card.dataset.copyKey = entry.key;
|
|
|
var meta = element('div', 'user-copy-page-editor-entry-meta');
|
|
|
meta.append(element('code', '', entry.key));
|
|
|
var bound = state.bindings.has(String(entry.key || ''));
|
|
|
if (bound) {
|
|
|
var locateButton = element('button', '', '定位文案');
|
|
|
locateButton.type = 'button';
|
|
|
locateButton.addEventListener('click', function () { locate(entry.key); });
|
|
|
meta.append(locateButton);
|
|
|
}
|
|
|
|
|
|
var sourceLabel = element('span', 'user-copy-page-editor-label', '中文源文案');
|
|
|
var source = element('p', 'user-copy-page-editor-source', entry.source);
|
|
|
var publishedLabel = element('span', 'user-copy-page-editor-label', '当前已发布译文');
|
|
|
var published = element('p', 'user-copy-page-editor-published',
|
|
|
entry.published && typeof entry.published.text === 'string'
|
|
|
? entry.published.text : '尚未发布');
|
|
|
var draftLabel = element('label', 'user-copy-page-editor-label', '目标语言草稿');
|
|
|
var textarea = element('textarea');
|
|
|
textarea.rows = 4;
|
|
|
textarea.maxLength = Number(entry.max_length) || 4000;
|
|
|
textarea.value = buffered.text;
|
|
|
textarea.id = 'user-copy-page-editor-' + String(entry.key).replace(/[^a-z0-9_-]/g, '-');
|
|
|
draftLabel.htmlFor = textarea.id;
|
|
|
textarea.readOnly = state.writeLocked || !workspace || workspace.write_ready !== true;
|
|
|
textarea.addEventListener('input', function () {
|
|
|
rememberDraft(entry, state.locale, textarea.value);
|
|
|
});
|
|
|
|
|
|
card.append(meta, sourceLabel, source, publishedLabel, published, draftLabel, textarea);
|
|
|
if (Array.isArray(entry.parameters) && entry.parameters.length) {
|
|
|
card.append(element('p', 'user-copy-page-editor-parameters',
|
|
|
'必须保留参数:' + entry.parameters.join('、')));
|
|
|
}
|
|
|
if (entry.draft && entry.draft.validation_state !== 'valid') {
|
|
|
var errors = Array.isArray(entry.draft.validation_errors)
|
|
|
? entry.draft.validation_errors : [];
|
|
|
card.append(element('p', 'user-copy-page-editor-validation',
|
|
|
errors.length ? errors.join(';') : '当前草稿未通过校验。'));
|
|
|
}
|
|
|
var save = element('button', 'user-copy-page-editor-save', '保存草稿');
|
|
|
save.type = 'button';
|
|
|
save.dataset.userCopySave = entry.key;
|
|
|
save.disabled = state.writeLocked || !workspace || workspace.write_ready !== true;
|
|
|
save.addEventListener('click', function () { saveDraft(entry, textarea, save); });
|
|
|
card.append(save);
|
|
|
return card;
|
|
|
}
|
|
|
|
|
|
function renderEntries() {
|
|
|
list.replaceChildren();
|
|
|
var entries = visibleEntries();
|
|
|
entries.forEach(function (entry) { list.append(renderEntry(entry)); });
|
|
|
if (entries.length === 0) {
|
|
|
list.append(element('p', 'user-copy-page-editor-empty',
|
|
|
state.activeTab === 'shared'
|
|
|
? '当前页面没有已绑定的公共框架文案。'
|
|
|
: '当前页面没有可编辑的正文文案。'));
|
|
|
}
|
|
|
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, 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 || '');
|
|
|
renderLocales();
|
|
|
renderEntries();
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
list.setAttribute('aria-busy', 'true');
|
|
|
loadMore.disabled = true;
|
|
|
inform((append ? '正在加载更多' : '正在读取')
|
|
|
+ tabLabel(channel) + '译文…', '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.catalog_query || '') === String(state.query || '').trim()) {
|
|
|
renderLocales();
|
|
|
renderEntries();
|
|
|
inform(tabLabel(tab) + '译文已加载。', 'info');
|
|
|
return;
|
|
|
}
|
|
|
loadWorkspace(tab, state.locale).catch(function (error) {
|
|
|
if (tab === state.activeTab) {
|
|
|
inform(error && error.message ? error.message : '页面译文读取失败。', 'error');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
async function saveDraft(entry, textarea, button) {
|
|
|
var workspace = activeWorkspace();
|
|
|
if (state.writeLocked || !workspace || workspace.write_ready !== true) return;
|
|
|
var locale = state.locale;
|
|
|
var text = textarea.value;
|
|
|
if (!text.trim()) {
|
|
|
inform('目标语言草稿不能为空。', 'error');
|
|
|
textarea.focus();
|
|
|
return;
|
|
|
}
|
|
|
button.disabled = true;
|
|
|
inform('正在保存草稿…', 'info');
|
|
|
var saved = null;
|
|
|
try {
|
|
|
var result = await transport.mutate('/admin/user-copy/entries/'
|
|
|
+ encodeURIComponent(entry.key) + '/draft', {
|
|
|
method: 'PUT',
|
|
|
body: {
|
|
|
locale: locale,
|
|
|
text: text,
|
|
|
hidden: false,
|
|
|
draft_version: Number(entry.draft_version) || 0,
|
|
|
},
|
|
|
});
|
|
|
saved = result && result.entry ? result.entry : null;
|
|
|
if (!saved || typeof saved.version !== 'number') {
|
|
|
lockWrites('草稿可能已保存,但响应协议无效,请刷新页面核对;请勿重复提交。');
|
|
|
return;
|
|
|
}
|
|
|
entry.draft = saved;
|
|
|
entry.draft_version = saved.version;
|
|
|
entry.hidden = saved.visibility === 'hidden';
|
|
|
if (!entry.published || entry.published.translated !== true) {
|
|
|
entry.state = 'draft';
|
|
|
}
|
|
|
clearBufferedDraft(locale, entry.key);
|
|
|
} catch (error) {
|
|
|
if (error && error.mutationMayHaveApplied === true) {
|
|
|
lockWrites('保存结果无法确认,请刷新页面核对;请勿重复提交。');
|
|
|
return;
|
|
|
}
|
|
|
if (error && error.status === 409) {
|
|
|
try { await loadWorkspace(state.activeTab, state.locale); }
|
|
|
catch (readError) { inform(readError.message || '最新草稿读取失败。', 'error'); return; }
|
|
|
inform('草稿已被其他管理员修改,已加载最新版本。', 'error');
|
|
|
return;
|
|
|
}
|
|
|
if (error && (error.status === 419 || error.status === 403)) {
|
|
|
lockWrites(error.status === 419
|
|
|
? '登录会话已失效,请刷新页面。'
|
|
|
: '管理权限已变化,请刷新页面。');
|
|
|
return;
|
|
|
}
|
|
|
if (error && error.status === 422) {
|
|
|
inform(error.message || '译文校验未通过,请修正后手动保存。', 'error');
|
|
|
button.disabled = false;
|
|
|
textarea.focus();
|
|
|
return;
|
|
|
}
|
|
|
inform(error && error.message ? error.message : '草稿保存失败。', 'error');
|
|
|
button.disabled = false;
|
|
|
return;
|
|
|
}
|
|
|
button.disabled = false;
|
|
|
if (saved.validation_state !== 'valid') {
|
|
|
var errors = Array.isArray(saved.validation_errors)
|
|
|
? saved.validation_errors : [];
|
|
|
inform('草稿已保存,但校验未通过'
|
|
|
+ (errors.length ? ':' + errors.join(';') : '。'), 'error');
|
|
|
} else {
|
|
|
inform('草稿已保存;公开页面不会在后台统一发布前改变。', 'success');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function openEditor() {
|
|
|
state.previousFocus = document.activeElement;
|
|
|
backdrop.hidden = false;
|
|
|
drawer.hidden = false;
|
|
|
document.body.classList.add('user-copy-page-editor-open');
|
|
|
close.focus();
|
|
|
loadWorkspace(state.activeTab, state.locale).catch(function (error) {
|
|
|
inform(error && error.message ? error.message : '页面译文读取失败。', 'error');
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function closeEditor() {
|
|
|
if (state.dirtyKeys.size > 0
|
|
|
&& !window.confirm('存在未保存输入,关闭编辑器后将丢弃这些输入。确认继续?')) {
|
|
|
return false;
|
|
|
}
|
|
|
discardAllDrafts();
|
|
|
abortAllReads();
|
|
|
clearSearchTimer();
|
|
|
drawer.hidden = true;
|
|
|
backdrop.hidden = true;
|
|
|
document.body.classList.remove('user-copy-page-editor-open');
|
|
|
removeHighlights();
|
|
|
if (state.previousFocus && typeof state.previousFocus.focus === 'function') {
|
|
|
state.previousFocus.focus();
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
launch.addEventListener('click', openEditor);
|
|
|
close.addEventListener('click', closeEditor);
|
|
|
backdrop.addEventListener('click', closeEditor);
|
|
|
[pageTab, sharedTab].forEach(function (button) {
|
|
|
button.addEventListener('click', function () {
|
|
|
switchTab(button.dataset.userCopyEditorTab);
|
|
|
});
|
|
|
});
|
|
|
tabs.addEventListener('keydown', function (event) {
|
|
|
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
|
|
|
event.preventDefault();
|
|
|
var tab = state.activeTab === 'page' ? 'shared' : 'page';
|
|
|
switchTab(tab);
|
|
|
(tab === 'shared' ? sharedTab : pageTab).focus();
|
|
|
});
|
|
|
localeSelect.addEventListener('change', function () {
|
|
|
var previousLocale = state.locale;
|
|
|
var nextLocale = localeSelect.value;
|
|
|
if (hasDirtyLocale(previousLocale)
|
|
|
&& !window.confirm('当前语言存在未保存输入,切换后将丢弃这些输入。确认继续?')) {
|
|
|
localeSelect.value = previousLocale;
|
|
|
return;
|
|
|
}
|
|
|
discardLocaleDrafts(previousLocale);
|
|
|
abortAllReads();
|
|
|
clearSearchTimer();
|
|
|
state.locale = nextLocale;
|
|
|
state.workspaces.page = null;
|
|
|
state.workspaces.shared = null;
|
|
|
loadWorkspace(state.activeTab, state.locale).catch(function (error) {
|
|
|
inform(error && error.message ? error.message : '目标语言读取失败。', 'error');
|
|
|
});
|
|
|
});
|
|
|
search.addEventListener('input', function () {
|
|
|
state.query = search.value;
|
|
|
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;
|
|
|
if (event.key === 'Escape') {
|
|
|
event.preventDefault();
|
|
|
closeEditor();
|
|
|
return;
|
|
|
}
|
|
|
if (event.key !== 'Tab') return;
|
|
|
var focusable = Array.from(drawer.querySelectorAll(
|
|
|
'button:not([disabled]),a[href],input:not([disabled]),select:not([disabled]),textarea:not([disabled])'
|
|
|
));
|
|
|
if (focusable.length === 0) return;
|
|
|
var first = focusable[0];
|
|
|
var last = focusable[focusable.length - 1];
|
|
|
if (event.shiftKey && document.activeElement === first) {
|
|
|
event.preventDefault(); last.focus();
|
|
|
} else if (!event.shiftKey && document.activeElement === last) {
|
|
|
event.preventDefault(); first.focus();
|
|
|
}
|
|
|
});
|
|
|
window.addEventListener('beforeunload', function (event) {
|
|
|
if (state.dirtyKeys.size === 0) return;
|
|
|
event.preventDefault();
|
|
|
event.returnValue = '';
|
|
|
});
|
|
|
updateTabUi();
|
|
|
}());
|