Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0609a10

Browse files
committed
more work on table capsules
1 parent fb2f22b commit 0609a10

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/gwt/panmirror/src/editor/src/nodes/table/table-capsule.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function tableBlockCapsuleFilter() : PandocBlockCapsuleFilter {
3131

3232
type: kTableBlockCapsuleType,
3333

34-
match: /^([\t >]*)(<table>.*?<\/table>)([ \t]*)$/gm,
34+
match: /^([\t >]*)(<table.*?<\/table>)([ \t]*)$/gm,
3535

3636
// textually enclose the capsule so that pandoc parses it as the type of block we want it to
3737
// (in this case we don't do anything because pandoc would have written this table as a
@@ -99,9 +99,24 @@ export function tableBlockCapsuleFilter() : PandocBlockCapsuleFilter {
9999
// get prosemirror dom parser
100100
const prosemirrorDomParser = DOMParser.fromSchema(schema);
101101

102+
// get unparsed (by prosemirror) table
103+
const unparsedTable = doc.body.firstChild;
104+
105+
// ensure that table cells all have content
106+
const ensureCellContent = (tag: string) => {
107+
const cells = unparsedTable.getElementsByTagName(tag);
108+
for (let i=0; i<cells.length; i++) {
109+
const cell = cells.item(i)!;
110+
if (cell.children.length === 0) {
111+
cell.append(window.document.createElement('p'));
112+
}
113+
}
114+
};
115+
ensureCellContent('td');
116+
ensureCellContent('th');
117+
102118
// extract caption element
103119
let captionElement: HTMLTableCaptionElement | null = null;
104-
const unparsedTable = doc.body.firstChild;
105120
const captionCollection = unparsedTable.getElementsByTagName('caption');
106121
if (captionCollection.length) {
107122
captionElement = captionCollection.item(0);

src/gwt/panmirror/src/editor/src/nodes/table/table-nodes.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ const nodes = tableNodes({
5252
return (dom as HTMLElement).className;
5353
},
5454
setDOMAttr(value, attrs) {
55-
if (value) {
56-
attrs.class = value;
57-
}
55+
attrs.class = kDefaultCellClasses;
5856
},
5957
},
6058
},
@@ -97,9 +95,30 @@ export const tableNode = {
9795
getAttrs: (dom: Node | string) => {
9896
const el = dom as HTMLElement;
9997

100-
let width: number | null = null;
98+
// shared colpercents
10199
let colpercents: number[] | null = null;
102100

101+
// if we have a colgroup w/ widths then read percents from there
102+
// <colgroup><col style="width: 44%" /><col style="width: 11%" /></colgroup>
103+
const colgroup = el.getElementsByTagName('colgroup');
104+
if (colgroup.length) {
105+
const cols = colgroup[0].childElementCount;
106+
colpercents = new Array<number>(cols).fill(0);
107+
for (let i = 0; i<cols; i++) {
108+
const col = colgroup[0].children.item(i) as HTMLElement;
109+
if (col.style.width) {
110+
colpercents[i] = (parseInt(col.style.width, 10) || 0) / 100;
111+
}
112+
}
113+
if (colpercents.every(value => !!value)) {
114+
return {
115+
colpercents
116+
};
117+
}
118+
}
119+
120+
// otherwise read from data-colwidth
121+
let width: number | null = null;
103122
const rows = el.getElementsByTagName('tr');
104123
if (rows.length) {
105124
const firstRow = rows.item(0)!;

0 commit comments

Comments
 (0)