Refine Page Linking workflow
This commit is contained in:
167
js/page-link.js
167
js/page-link.js
@@ -43,7 +43,7 @@
|
||||
|
||||
function requestPageLink(cmd, params) {
|
||||
var pageId = getPageId();
|
||||
if (!pageId) return false;
|
||||
if (!pageId) return Promise.reject(new Error('missing page id'));
|
||||
|
||||
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
window.fetch(endpoint, {
|
||||
return window.fetch(endpoint, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
||||
@@ -68,13 +68,7 @@
|
||||
}
|
||||
return body;
|
||||
});
|
||||
}).catch(function (err) {
|
||||
if (window.console && window.console.warn) {
|
||||
window.console.warn('PageLink request failed:', err);
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function ensurePageLink() {
|
||||
@@ -85,44 +79,38 @@
|
||||
return requestPageLink('unlink', { sectok: getSectok() });
|
||||
}
|
||||
|
||||
function copyToClipboard(text) {
|
||||
if (!text) return;
|
||||
if (window.navigator && window.navigator.clipboard && window.navigator.clipboard.writeText) {
|
||||
window.navigator.clipboard.writeText(text).catch(function () {});
|
||||
return;
|
||||
}
|
||||
|
||||
function triggerDownload(pageId) {
|
||||
try {
|
||||
var textarea = document.createElement('textarea');
|
||||
textarea.value = text;
|
||||
textarea.setAttribute('readonly', 'readonly');
|
||||
textarea.style.position = 'absolute';
|
||||
textarea.style.left = '-9999px';
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
} catch (e) {}
|
||||
document.body.removeChild(textarea);
|
||||
} catch (e2) {}
|
||||
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
||||
var href = endpoint + '?cmd=download&id=' + encodeURIComponent(pageId);
|
||||
|
||||
var a = document.createElement('a');
|
||||
a.href = href;
|
||||
a.download = '.pagelink';
|
||||
a.style.display = 'none';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
function attachCopyTargets() {
|
||||
var targets = document.querySelectorAll('[data-luxtools-pagelink-copy="1"]');
|
||||
if (!targets || !targets.length) return;
|
||||
function fetchPageLinkInfo(pageId) {
|
||||
if (!pageId) return Promise.reject(new Error('missing page id'));
|
||||
|
||||
targets.forEach(function (el) {
|
||||
if (!el || !el.getAttribute) return;
|
||||
el.setAttribute('role', 'button');
|
||||
el.setAttribute('tabindex', '0');
|
||||
el.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
copyToClipboard(String(el.getAttribute('data-uuid') || '').trim());
|
||||
});
|
||||
el.addEventListener('keydown', function (e) {
|
||||
if (!e || (e.key !== 'Enter' && e.key !== ' ')) return;
|
||||
e.preventDefault();
|
||||
copyToClipboard(String(el.getAttribute('data-uuid') || '').trim());
|
||||
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
||||
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
|
||||
|
||||
return window.fetch(query, {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin'
|
||||
}).then(function (res) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
if (!res.ok || !body || body.ok !== true) {
|
||||
throw new Error('request failed');
|
||||
}
|
||||
return body;
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -136,25 +124,45 @@
|
||||
var pageId = getPageId();
|
||||
if (!pageId) return;
|
||||
|
||||
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
||||
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
|
||||
|
||||
window.fetch(query, {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin'
|
||||
}).then(function (res) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
if (!res.ok || !body || body.ok !== true) {
|
||||
throw new Error('request failed');
|
||||
}
|
||||
return body;
|
||||
});
|
||||
}).then(function (info) {
|
||||
if (!info || !info.uuid) return;
|
||||
|
||||
fetchPageLinkInfo(pageId).then(function (info) {
|
||||
var link = document.createElement('a');
|
||||
link.href = endpoint + '?cmd=download&id=' + encodeURIComponent(pageId);
|
||||
link.textContent = 'Page ID: ' + String(info.uuid);
|
||||
link.href = '#';
|
||||
|
||||
if (!info || !info.uuid) {
|
||||
link.textContent = 'Link Page';
|
||||
link.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
ensurePageLink().then(function (res) {
|
||||
if (!res || !res.uuid) throw new Error('no uuid');
|
||||
triggerDownload(pageId);
|
||||
}).catch(function (err) {
|
||||
if (window.console && window.console.warn) {
|
||||
window.console.warn('PageLink ensure failed:', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if (info.linked) {
|
||||
link.textContent = 'Unlink Page';
|
||||
link.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
if (!window.confirm('Unlink page?')) return;
|
||||
unlinkPageLink().then(function () {
|
||||
window.setTimeout(function () {
|
||||
try { window.location.reload(); } catch (e2) {}
|
||||
}, 400);
|
||||
}).catch(function (err) {
|
||||
if (window.console && window.console.warn) {
|
||||
window.console.warn('PageLink unlink failed:', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
link.textContent = 'Download Link File';
|
||||
link.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
triggerDownload(pageId);
|
||||
});
|
||||
}
|
||||
|
||||
var first = container.firstChild;
|
||||
container.insertBefore(link, first);
|
||||
@@ -166,46 +174,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
window.addBtnActionLuxtoolsPageLink = function ($btn, props, edid) {
|
||||
$btn.on('click', function () {
|
||||
var pageId = getPageId();
|
||||
if (!pageId) return false;
|
||||
|
||||
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
||||
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
|
||||
|
||||
window.fetch(query, {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin'
|
||||
}).then(function (res) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
if (!res.ok || !body || body.ok !== true) {
|
||||
throw new Error('request failed');
|
||||
}
|
||||
return body;
|
||||
});
|
||||
}).then(function (info) {
|
||||
if (info && info.uuid) {
|
||||
if (window.confirm('Unlink page?')) {
|
||||
unlinkPageLink();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ensurePageLink();
|
||||
}).catch(function (err) {
|
||||
if (window.console && window.console.warn) {
|
||||
window.console.warn('PageLink info failed:', err);
|
||||
}
|
||||
ensurePageLink();
|
||||
});
|
||||
return false;
|
||||
});
|
||||
return 'luxtools-pagelink';
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
attachCopyTargets();
|
||||
attachDocInfoLink();
|
||||
}, false);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user