/** * 统一富文本编辑框:斜体 / 粗体 / 清除格式 / 粘贴 / 复制。 * 页面通过宏 templates/macros/rich_text.html 生成 DOM,本模块提供行为。 */ (function (global) { 'use strict'; function _el(editorId) { return document.getElementById(editorId); } function toggleFormat(editorId, format) { const element = _el(editorId); if (!element) return; element.focus(); if (format === 'italic') { document.execCommand('italic', false, null); } else if (format === 'bold') { document.execCommand('bold', false, null); } onInput(editorId); } function clearFormat(editorId) { const element = _el(editorId); if (!element) return; element.focus(); document.execCommand('removeFormat', false, null); onInput(editorId); } function onInput(editorId) { const element = _el(editorId); if (!element) return; const hidden = document.getElementById(editorId + '_hidden'); if (hidden) hidden.value = element.innerHTML; try { element.dispatchEvent(new CustomEvent('iar-rich-input', { bubbles: true, detail: { id: editorId, html: element.innerHTML, text: element.innerText || element.textContent || '' } })); } catch (e) { /* ignore */ } } function handlePaste(event, editorId) { setTimeout(function () { onInput(editorId); }, 10); } function getHtml(editorId) { const element = _el(editorId); return element ? (element.innerHTML || '') : ''; } function getText(editorId) { const element = _el(editorId); if (!element) return ''; return element.innerText || element.textContent || ''; } function setHtml(editorId, html) { const element = _el(editorId); if (!element) return; element.innerHTML = html == null ? '' : String(html); onInput(editorId); } function _createStyledHtml(htmlContent, fontFamily, fontSize) { const font = fontFamily || 'Times New Roman'; const size = fontSize || '12pt'; return ( '' + htmlContent + '' ); } function _copyPlain(element, onSuccess, onFail) { const textContent = element.innerText || element.textContent || ''; const done = function () { if (typeof onSuccess === 'function') onSuccess(); }; const fail = function () { if (typeof onFail === 'function') onFail(); else alert('复制失败,请手动选中内容后使用 Ctrl+C(或右键复制)'); }; if (typeof global.copyTextToClipboard === 'function') { global.copyTextToClipboard(textContent).then(done).catch(fail); return; } if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(textContent).then(done).catch(fail); return; } fail(); } function _fallbackCopy(element, opts) { const font = (opts && opts.fontFamily) || 'Times New Roman'; const size = (opts && opts.fontSize) || '12pt'; const onSuccess = opts && opts.onSuccess; const tempDiv = document.createElement('div'); tempDiv.innerHTML = element.innerHTML; tempDiv.style.fontFamily = '"' + font + '", serif'; tempDiv.style.fontSize = size; tempDiv.style.lineHeight = '1.5'; tempDiv.style.position = 'absolute'; tempDiv.style.left = '-9999px'; tempDiv.style.top = '-9999px'; document.body.appendChild(tempDiv); const range = document.createRange(); range.selectNodeContents(tempDiv); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); try { const successful = document.execCommand('copy'); if (successful) { if (typeof onSuccess === 'function') onSuccess(); } else { _copyPlain(element, onSuccess, opts && opts.onFail); } } catch (err) { _copyPlain(element, onSuccess, opts && opts.onFail); } setTimeout(function () { try { selection.removeAllRanges(); } catch (e1) {} try { if (tempDiv.parentNode) tempDiv.parentNode.removeChild(tempDiv); } catch (e2) {} }, 120); } function copyFormatted(editorId, opts) { opts = opts || {}; const element = _el(editorId); if (!element) { alert('找不到要复制的元素'); return; } const onSuccess = opts.onSuccess || function () { if (typeof global.showCopyMessage === 'function') global.showCopyMessage('已复制到剪贴板'); }; const fontFamily = opts.fontFamily; const fontSize = opts.fontSize; if (navigator.clipboard && global.ClipboardItem) { try { const styledHtml = _createStyledHtml(element.innerHTML, fontFamily, fontSize); const htmlBlob = new Blob([styledHtml], { type: 'text/html' }); const textBlob = new Blob([element.innerText || element.textContent || ''], { type: 'text/plain' }); const clipboardItem = new ClipboardItem({ 'text/html': htmlBlob, 'text/plain': textBlob }); navigator.clipboard.write([clipboardItem]).then(onSuccess).catch(function () { _fallbackCopy(element, Object.assign({}, opts, { onSuccess: onSuccess })); }); } catch (err) { _fallbackCopy(element, Object.assign({}, opts, { onSuccess: onSuccess })); } } else { _fallbackCopy(element, Object.assign({}, opts, { onSuccess: onSuccess })); } } const api = { toggleFormat: toggleFormat, clearFormat: clearFormat, onInput: onInput, handlePaste: handlePaste, getHtml: getHtml, getText: getText, setHtml: setHtml, copyFormatted: copyFormatted, }; global.IARRichText = api; if (typeof global.toggleFormat !== 'function') global.toggleFormat = toggleFormat; if (typeof global.clearFormat !== 'function') global.clearFormat = clearFormat; })(typeof window !== 'undefined' ? window : this);