Commit 6dbac17f by Яков

update table insert

parent f9e8ca8c
{
"name": "react-ag-qeditor",
"version": "1.0.60",
"version": "1.0.61",
"description": "WYSIWYG html editor",
"author": "atma",
"license": "MIT",
......
......@@ -455,6 +455,54 @@ const QEditor = ({
// }
// }
}
function cleanWordHtml(html) {
return html
// Удаляем MS Office специфичные теги
.replace(/<(!|meta|link|style|o:[^>]+)>/gi, '')
// Удаляем ненужные атрибуты
.replace(/\s(class|style|lang)=("|')[^"']*("|')/gi, '')
// Заменяем MS теги на стандартные
.replace(/<(\/?)v:[^>]+>/g, '<$1span>')
// Удаляем пустые параграфы
.replace(/<p[^>]*>( |\s)*<\/p>/g, '')
}
function parseWordTable(html) {
const doc = new DOMParser().parseFromString(html, 'text/html')
const tables = doc.querySelectorAll('table')
if (!tables.length) return null
const table = tables[0]
const rows = []
table.querySelectorAll('tr').forEach(tr => {
const cells = []
tr.querySelectorAll('td, th').forEach(td => {
cells.push({
type: 'tableCell',
content: [{
type: 'paragraph',
content: td.textContent ? [{ type: 'text', text: td.textContent }] : []
}]
})
})
rows.push({
type: 'tableRow',
content: cells
})
})
return {
type: 'table',
content: [
{
type: 'tableRow',
content: rows[0].content.map(() => ({ type: 'tableHeader' }))
},
...rows
]
}
}
const editor = useEditor({
extensions: [
......@@ -525,6 +573,26 @@ const QEditor = ({
wrap.querySelectorAll('.atma-editor-toolbar-s').forEach(function (s) {
s.classList.remove('show')
})
},
editorProps: {
handlePaste: (view, event) => {
const html = event.clipboardData?.getData('text/html')
// Если вставляется таблица из Word
if (html?.includes('urn:schemas-microsoft-com:office:word')) {
const cleanedHtml = cleanWordHtml(html) // Функция очистки (см. ниже)
const tr = view.state.tr
tr.replaceSelectionWith(view.state.schema.nodeFromJSON({
type: 'table',
content: parseWordTable(cleanedHtml) // Функция парсинга таблицы
}))
view.dispatch(tr)
return true // Отменяем стандартную обработку
}
},
transformPastedHTML(html) {
return cleanWordHtml(html)
}
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment