UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 4.93 kB
{ "version": 3, "sources": ["../../src/table/transforms.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createBlock } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { normalizeRowColSpan } from './utils';\n\nconst tableContentPasteSchema = ( { phrasingContentSchema } ) => ( {\n\ttr: {\n\t\tallowEmpty: true,\n\t\tchildren: {\n\t\t\tth: {\n\t\t\t\tallowEmpty: true,\n\t\t\t\tchildren: phrasingContentSchema,\n\t\t\t\tattributes: [ 'scope', 'colspan', 'rowspan', 'style' ],\n\t\t\t},\n\t\t\ttd: {\n\t\t\t\tallowEmpty: true,\n\t\t\t\tchildren: phrasingContentSchema,\n\t\t\t\tattributes: [ 'colspan', 'rowspan', 'style' ],\n\t\t\t},\n\t\t},\n\t},\n} );\n\nconst tablePasteSchema = ( args ) => ( {\n\ttable: {\n\t\tchildren: {\n\t\t\tthead: {\n\t\t\t\tallowEmpty: true,\n\t\t\t\tchildren: tableContentPasteSchema( args ),\n\t\t\t},\n\t\t\ttfoot: {\n\t\t\t\tallowEmpty: true,\n\t\t\t\tchildren: tableContentPasteSchema( args ),\n\t\t\t},\n\t\t\ttbody: {\n\t\t\t\tallowEmpty: true,\n\t\t\t\tchildren: tableContentPasteSchema( args ),\n\t\t\t},\n\t\t},\n\t},\n} );\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'raw',\n\t\t\tselector: 'table',\n\t\t\tschema: tablePasteSchema,\n\t\t\ttransform: ( node ) => {\n\t\t\t\tconst attributes = Array.from( node.children ).reduce(\n\t\t\t\t\t( sectionAcc, section ) => {\n\t\t\t\t\t\tif ( ! section.children.length ) {\n\t\t\t\t\t\t\treturn sectionAcc;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst sectionName = section.nodeName\n\t\t\t\t\t\t\t.toLowerCase()\n\t\t\t\t\t\t\t.slice( 1 );\n\n\t\t\t\t\t\tconst sectionAttributes = Array.from(\n\t\t\t\t\t\t\tsection.children\n\t\t\t\t\t\t).reduce( ( rowAcc, row ) => {\n\t\t\t\t\t\t\tif ( ! row.children.length ) {\n\t\t\t\t\t\t\t\treturn rowAcc;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst rowAttributes = Array.from(\n\t\t\t\t\t\t\t\trow.children\n\t\t\t\t\t\t\t).reduce( ( colAcc, col ) => {\n\t\t\t\t\t\t\t\tconst rowspan = normalizeRowColSpan(\n\t\t\t\t\t\t\t\t\tcol.getAttribute( 'rowspan' )\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tconst colspan = normalizeRowColSpan(\n\t\t\t\t\t\t\t\t\tcol.getAttribute( 'colspan' )\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tconst { textAlign } = col.style || {};\n\n\t\t\t\t\t\t\t\tlet align;\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\ttextAlign === 'left' ||\n\t\t\t\t\t\t\t\t\ttextAlign === 'center' ||\n\t\t\t\t\t\t\t\t\ttextAlign === 'right'\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\talign = textAlign;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tcolAcc.push( {\n\t\t\t\t\t\t\t\t\ttag: col.nodeName.toLowerCase(),\n\t\t\t\t\t\t\t\t\tcontent: col.innerHTML,\n\t\t\t\t\t\t\t\t\trowspan,\n\t\t\t\t\t\t\t\t\tcolspan,\n\t\t\t\t\t\t\t\t\talign,\n\t\t\t\t\t\t\t\t} );\n\n\t\t\t\t\t\t\t\treturn colAcc;\n\t\t\t\t\t\t\t}, [] );\n\n\t\t\t\t\t\t\trowAcc.push( {\n\t\t\t\t\t\t\t\tcells: rowAttributes,\n\t\t\t\t\t\t\t} );\n\n\t\t\t\t\t\t\treturn rowAcc;\n\t\t\t\t\t\t}, [] );\n\n\t\t\t\t\t\tsectionAcc[ sectionName ] = sectionAttributes;\n\t\t\t\t\t\treturn sectionAcc;\n\t\t\t\t\t},\n\t\t\t\t\t{}\n\t\t\t\t);\n\n\t\t\t\treturn createBlock( 'core/table', attributes );\n\t\t\t},\n\t\t},\n\t],\n};\n\nexport default transforms;\n"], "mappings": ";AAGA,SAAS,mBAAmB;AAK5B,SAAS,2BAA2B;AAEpC,IAAM,0BAA0B,CAAE,EAAE,sBAAsB,OAAS;AAAA,EAClE,IAAI;AAAA,IACH,YAAY;AAAA,IACZ,UAAU;AAAA,MACT,IAAI;AAAA,QACH,YAAY;AAAA,QACZ,UAAU;AAAA,QACV,YAAY,CAAE,SAAS,WAAW,WAAW,OAAQ;AAAA,MACtD;AAAA,MACA,IAAI;AAAA,QACH,YAAY;AAAA,QACZ,UAAU;AAAA,QACV,YAAY,CAAE,WAAW,WAAW,OAAQ;AAAA,MAC7C;AAAA,IACD;AAAA,EACD;AACD;AAEA,IAAM,mBAAmB,CAAE,UAAY;AAAA,EACtC,OAAO;AAAA,IACN,UAAU;AAAA,MACT,OAAO;AAAA,QACN,YAAY;AAAA,QACZ,UAAU,wBAAyB,IAAK;AAAA,MACzC;AAAA,MACA,OAAO;AAAA,QACN,YAAY;AAAA,QACZ,UAAU,wBAAyB,IAAK;AAAA,MACzC;AAAA,MACA,OAAO;AAAA,QACN,YAAY;AAAA,QACZ,UAAU,wBAAyB,IAAK;AAAA,MACzC;AAAA,IACD;AAAA,EACD;AACD;AAEA,IAAM,aAAa;AAAA,EAClB,MAAM;AAAA,IACL;AAAA,MACC,MAAM;AAAA,MACN,UAAU;AAAA,MACV,QAAQ;AAAA,MACR,WAAW,CAAE,SAAU;AACtB,cAAM,aAAa,MAAM,KAAM,KAAK,QAAS,EAAE;AAAA,UAC9C,CAAE,YAAY,YAAa;AAC1B,gBAAK,CAAE,QAAQ,SAAS,QAAS;AAChC,qBAAO;AAAA,YACR;AAEA,kBAAM,cAAc,QAAQ,SAC1B,YAAY,EACZ,MAAO,CAAE;AAEX,kBA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