"മീഡിയവിക്കി:Gadget-WikiEditMobile.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
fixing toolbar and buttons |
add more functions |
||
| വരി 245: | വരി 245: | ||
'#wem-dlg-insert{background:#3366cc;color:#fff}', | '#wem-dlg-insert{background:#3366cc;color:#fff}', | ||
'#wem-dlg-insert:disabled{opacity:.5}', | '#wem-dlg-insert:disabled{opacity:.5}', | ||
/* Preview / diff pane — swaps in place of the editable surface */ | |||
'#wem-preview{flex:1;overflow-y:auto;-webkit-overflow-scrolling:touch;padding:12px;min-height:0;word-wrap:break-word;line-height:1.6;font-size:15px}', | |||
'#wem-preview[hidden]{display:none}', | |||
'#wem-preview table.diff{width:100%;border-collapse:collapse;font-size:12px;table-layout:fixed}', | |||
'#wem-preview col.diff-marker{width:20px}', | |||
'#wem-preview col.diff-content{width:45%}', | |||
'#wem-preview .diff-marker{text-align:center;color:#72777d;padding:1px 4px;font-family:monospace}', | |||
'#wem-preview .diff-addedline{background:#d4f0d4}', | |||
'#wem-preview .diff-deletedline{background:#ffd4d4}', | |||
'#wem-preview td{padding:2px 4px;vertical-align:top;word-break:break-word;white-space:pre-wrap}', | |||
'#wem-preview .diff-empty{background:#eaecf0}', | |||
'#wem-preview .diffchange{background:#a3d1a3}', | |||
'#wem-preview .diffchange-inline{background:#ffd0d0}', | |||
/* Small secondary buttons in the summary row (Preview / Diff) */ | |||
'.wem-action-btn{flex-shrink:0;padding:5px 8px;font-size:12px;font-weight:600;border-radius:4px;cursor:pointer;touch-action:manipulation;border:1px solid #c8ccd1;background:#f8f9fa;color:#202122;white-space:nowrap}', | |||
'.wem-action-btn:active{background:#e0e2e8}', | |||
].join(''); | ].join(''); | ||
| വരി 824: | വരി 841: | ||
function escHtml(s) { | function escHtml(s) { | ||
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); | return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); | ||
} | |||
// Remove <br> artifacts that contentEditable inserts in table cells and | |||
// between block elements. Called before html→wikitext conversion so these | |||
// don't appear as <br /> in the saved wikitext. | |||
function sanitizeForSave(root) { | |||
// Strip <br> at the very start or end of every table cell / caption | |||
root.querySelectorAll('td, th, caption').forEach(function (cell) { | |||
while (cell.firstChild && cell.firstChild.nodeName === 'BR') { | |||
cell.removeChild(cell.firstChild); | |||
} | |||
while (cell.lastChild && cell.lastChild.nodeName === 'BR') { | |||
cell.removeChild(cell.lastChild); | |||
} | |||
}); | |||
// Remove <p><br></p> — the empty-line placeholder contentEditable inserts | |||
root.querySelectorAll('p').forEach(function (p) { | |||
if (p.childNodes.length === 1 && p.firstChild.nodeName === 'BR') { | |||
p.parentNode.removeChild(p); | |||
} | |||
}); | |||
// Remove bare <br> that sit directly between two block-level elements | |||
var BLOCK = /^(P|DIV|H[1-6]|UL|OL|LI|TABLE|TR|TD|TH|BLOCKQUOTE|PRE|FIGURE)$/; | |||
root.querySelectorAll('br').forEach(function (br) { | |||
var prev = br.previousSibling; | |||
var next = br.nextSibling; | |||
var prevBlock = !prev || (prev.nodeType === 1 && BLOCK.test(prev.nodeName)); | |||
var nextBlock = !next || (next.nodeType === 1 && BLOCK.test(next.nodeName)); | |||
if (prevBlock && nextBlock) { | |||
br.parentNode.removeChild(br); | |||
} | |||
}); | |||
} | } | ||
| വരി 830: | വരി 881: | ||
// ────────────────────────────────────────────────────────────────────────────── | // ────────────────────────────────────────────────────────────────────────────── | ||
var editor | var editor = null; // MobileEditor instance | ||
var curSection = null; // currently open section descriptor | var curSection = null; // currently open section descriptor | ||
var curView | var originalWikitext = null; // wikitext as-fetched; used for diff comparison | ||
var curView = 'sections'; // 'sections' | 'editor' | 'preview' | |||
function setStatus(msg, type) { | function setStatus(msg, type) { | ||
| വരി 850: | വരി 902: | ||
document.getElementById('wem-save').hidden = true; | document.getElementById('wem-save').hidden = true; | ||
document.getElementById('wem-back').style.visibility = 'hidden'; | document.getElementById('wem-back').style.visibility = 'hidden'; | ||
document.getElementById('wem-preview').hidden = true; | |||
originalWikitext = null; | |||
setStatus(''); | setStatus(''); | ||
if (editor) { editor.setContent(''); } | if (editor) { editor.setContent(''); } | ||
| വരി 858: | വരി 912: | ||
curSection = section; | curSection = section; | ||
document.getElementById('wem-sections').hidden | document.getElementById('wem-sections').hidden = true; | ||
document.getElementById('wem-editor').hidden | document.getElementById('wem-editor').hidden = false; | ||
document.getElementById('wem-toolbar').hidden | document.getElementById('wem-toolbar').hidden = false; | ||
document.getElementById('wem-summary-row').hidden = false; | document.getElementById('wem-summary-row').hidden = false; | ||
document.getElementById('wem-summary').value | document.getElementById('wem-editable-wrap').hidden = false; | ||
document.getElementById('wem-preview').hidden = true; | |||
document.getElementById('wem-summary').value = ''; | |||
document.getElementById('wem-hdr-title').textContent = section.line || 'Introduction'; | document.getElementById('wem-hdr-title').textContent = section.line || 'Introduction'; | ||
document.getElementById('wem-back').style.visibility = 'visible'; | document.getElementById('wem-back').style.visibility = 'visible'; | ||
| വരി 871: | വരി 927: | ||
getSectionWikitext(section.index) | getSectionWikitext(section.index) | ||
.then(function (wt) { | .then(function (wt) { | ||
originalWikitext = wt; | |||
setStatus('Converting to HTML via Parsoid…'); | setStatus('Converting to HTML via Parsoid…'); | ||
return wikitextToHtml(wt); | return wikitextToHtml(wt); | ||
| വരി 892: | വരി 949: | ||
setStatus('Converting back to wikitext…'); | setStatus('Converting back to wikitext…'); | ||
var | var scratch = document.createElement('div'); | ||
scratch.innerHTML = editor.getContent(); | |||
sanitizeForSave(scratch); | |||
var html = scratch.innerHTML; | |||
var summary = document.getElementById('wem-summary').value.trim(); | var summary = document.getElementById('wem-summary').value.trim(); | ||
| വരി 913: | വരി 973: | ||
setStatus('⚠ Save failed: ' + err.message, 'err'); | setStatus('⚠ Save failed: ' + err.message, 'err'); | ||
saveBtn.disabled = false; | saveBtn.disabled = false; | ||
}); | |||
} | |||
// ── Preview / diff pane ─────────────────────────────────────────────────────── | |||
function showPreviewPane(html, mode) { | |||
var previewEl = document.getElementById('wem-preview'); | |||
previewEl.innerHTML = html; | |||
previewEl.hidden = false; | |||
document.getElementById('wem-editable-wrap').hidden = true; | |||
document.getElementById('wem-toolbar').hidden = true; | |||
document.getElementById('wem-summary-row').hidden = true; | |||
document.getElementById('wem-save').hidden = true; | |||
document.getElementById('wem-hdr-title').textContent = mode === 'diff' ? 'Differences' : 'Preview'; | |||
curView = 'preview'; | |||
} | |||
function showEditPane() { | |||
document.getElementById('wem-preview').hidden = true; | |||
document.getElementById('wem-editable-wrap').hidden = false; | |||
document.getElementById('wem-toolbar').hidden = false; | |||
document.getElementById('wem-summary-row').hidden = false; | |||
document.getElementById('wem-save').hidden = false; | |||
document.getElementById('wem-hdr-title').textContent = curSection ? (curSection.line || 'Introduction') : 'Edit article'; | |||
curView = 'editor'; | |||
} | |||
function handlePreview() { | |||
if (!curSection || !editor || !originalWikitext) return; | |||
setStatus('Generating preview…'); | |||
var scratch = document.createElement('div'); | |||
scratch.innerHTML = editor.getContent(); | |||
sanitizeForSave(scratch); | |||
htmlToWikitext(scratch.innerHTML) | |||
.then(function (wt) { | |||
return mwApi.get({ | |||
action: 'parse', | |||
title: PAGE, | |||
prop: 'text', | |||
pst: 'true', | |||
disablelimitreport: 'true', | |||
disableeditsection: 'true', | |||
sectionpreview: 'true', | |||
disabletoc: 'true', | |||
text: wt, | |||
format: 'json', | |||
}); | |||
}) | |||
.then(function (r) { | |||
showPreviewPane(r.parse.text['*'] + '<div style="clear:both"></div>', 'preview'); | |||
setStatus('Preview — tap ← to continue editing'); | |||
}) | |||
['catch'](function (err) { | |||
setStatus('⚠ Preview failed: ' + err.message, 'err'); | |||
}); | |||
} | |||
function handleCompare() { | |||
if (!curSection || !editor || !originalWikitext) return; | |||
setStatus('Generating diff…'); | |||
var scratch = document.createElement('div'); | |||
scratch.innerHTML = editor.getContent(); | |||
sanitizeForSave(scratch); | |||
htmlToWikitext(scratch.innerHTML) | |||
.then(function (edited) { | |||
return mwApi.post({ | |||
action: 'compare', | |||
fromslots: 'main', | |||
'fromtext-main': originalWikitext, | |||
fromtitle: PAGE, | |||
frompst: 'true', | |||
toslots: 'main', | |||
'totext-main': edited, | |||
totitle: PAGE, | |||
topst: 'true', | |||
format: 'json', | |||
}); | |||
}) | |||
.then(function (r) { | |||
if (!r.compare['*']) { | |||
setStatus('No differences from current version.', 'ok'); | |||
return; | |||
} | |||
var html = '<table class="diff"><colgroup>' + | |||
'<col class="diff-marker"><col class="diff-content">' + | |||
'<col class="diff-marker"><col class="diff-content">' + | |||
'</colgroup><tbody>' + r.compare['*'] + '</tbody></table>'; | |||
showPreviewPane(html, 'diff'); | |||
setStatus('Diff — tap ← to continue editing'); | |||
}) | |||
['catch'](function (err) { | |||
setStatus('⚠ Compare failed: ' + err.message, 'err'); | |||
}); | }); | ||
} | } | ||
| വരി 959: | വരി 1,111: | ||
editWrap.appendChild(editableDiv); | editWrap.appendChild(editableDiv); | ||
editorPane.appendChild(editWrap); | editorPane.appendChild(editWrap); | ||
var previewPane = ce('div', { id: 'wem-preview' }); | |||
previewPane.hidden = true; | |||
editorPane.appendChild(previewPane); | |||
// ── Edit summary row ── | // ── Edit summary row ── | ||
| വരി 965: | വരി 1,121: | ||
var summaryInput = ce('input', { id: 'wem-summary', type: 'text' }); | var summaryInput = ce('input', { id: 'wem-summary', type: 'text' }); | ||
summaryInput.placeholder = 'Edit summary (optional)…'; | summaryInput.placeholder = 'Edit summary (optional)…'; | ||
var previewBtn = ce('button', { 'class': 'wem-action-btn', type: 'button', title: 'Preview rendered output' }); | |||
previewBtn.textContent = 'Preview'; | |||
var compareBtn = ce('button', { 'class': 'wem-action-btn', type: 'button', title: 'Compare with saved version' }); | |||
compareBtn.textContent = 'Diff'; | |||
summaryRow.appendChild(summaryInput); | summaryRow.appendChild(summaryInput); | ||
summaryRow.appendChild(previewBtn); | |||
summaryRow.appendChild(compareBtn); | |||
// ── Status bar ── | // ── Status bar ── | ||
| വരി 994: | വരി 1,158: | ||
back.addEventListener('click', function () { | back.addEventListener('click', function () { | ||
if (!backdrop.hidden) { closeDialog(); return; } | if (!backdrop.hidden) { closeDialog(); return; } | ||
if (curView === 'preview') { showEditPane(); return; } | |||
showSectionsView(); | showSectionsView(); | ||
}); | }); | ||
| വരി 1,003: | വരി 1,168: | ||
saveBtn.addEventListener('click', handleSave); | saveBtn.addEventListener('click', handleSave); | ||
previewBtn.addEventListener('click', handlePreview); | |||
compareBtn.addEventListener('click', handleCompare); | |||
// Hardware back / Escape key | // Hardware back / Escape key | ||
document.addEventListener('keydown', function (e) { | document.addEventListener('keydown', function (e) { | ||
if (e.key === 'Escape' && !overlay.hidden) { | if (e.key === 'Escape' && !overlay.hidden) { | ||
if (curView === 'editor') { showSectionsView(); } | if (curView === 'preview') { showEditPane(); } | ||
else | else if (curView === 'editor') { showSectionsView(); } | ||
else { overlay.hidden = true; } | |||
} | } | ||
}); | }); | ||