Commit abeda839 authored by Fred Chasen's avatar Fred Chasen

Update counters to allow page and footnote resets

parent 1431319b
Pipeline #725 failed with stage
in 6 minutes and 27 seconds
This diff is collapsed.
...@@ -5,7 +5,7 @@ import json from 'rollup-plugin-json'; ...@@ -5,7 +5,7 @@ import json from 'rollup-plugin-json';
// import builtins from 'rollup-plugin-node-builtins'; // import builtins from 'rollup-plugin-node-builtins';
// import globals from 'rollup-plugin-node-globals'; // import globals from 'rollup-plugin-node-globals';
import pkg from './package.json'; // import pkg from './package.json';
import serve from 'rollup-plugin-serve' import serve from 'rollup-plugin-serve'
import livereload from 'rollup-plugin-livereload' import livereload from 'rollup-plugin-livereload'
......
...@@ -14,17 +14,28 @@ class Counters extends Handler { ...@@ -14,17 +14,28 @@ class Counters extends Handler {
let property = declaration.property; let property = declaration.property;
if (property === "counter-increment") { if (property === "counter-increment") {
let inc = this.handleIncrement(declaration, rule); this.handleIncrement(declaration, rule);
if (inc) { // clean up empty declaration
let hasProperities = false;
declaration.value.children.forEach((data) => {
if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
}
});
if (!hasProperities) {
dList.remove(dItem); dList.remove(dItem);
} }
} else if (property === "counter-reset") { } else if (property === "counter-reset") {
let reset = this.handleReset(declaration, rule); this.handleReset(declaration, rule);
for (const r of reset) { // clean up empty declaration
if (r.selector !== ".pagedjs_page") { let hasProperities = false;
dList.remove(dItem); declaration.value.children.forEach((data) => {
break; if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
} }
});
if (!hasProperities) {
dList.remove(dItem);
} }
} }
} }
...@@ -49,82 +60,112 @@ class Counters extends Handler { ...@@ -49,82 +60,112 @@ class Counters extends Handler {
} }
handleIncrement(declaration, rule) { handleIncrement(declaration, rule) {
let names = [];
let values = [];
let increments = []; let increments = [];
declaration.value.children.forEach((child) => { let children = declaration.value.children;
if (child.type && child.type === "Identifier") {
names.push(child.name);
}
if (child.type && child.type === "Number") {
values.push(child.value);
}
});
for (const name of names) {
let number = values.shift();
if (name === "page" || name.indexOf("target-counter-") === 0) { children.forEach((data, item) => {
continue; if (data.type && data.type === "Identifier") {
} let name = data.name;
let selector = csstree.generate(rule.ruleNode.prelude); if (name === "page" || name.indexOf("target-counter-") === 0) {
return;
}
let counter; let whitespace, number, value;
if (!(name in this.counters)) { if (item.next && item.next.data.type === "WhiteSpace") {
counter = this.addCounter(name); whitespace = item.next;
} else { }
counter = this.counters[name]; if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
number = whitespace.next;
value = parseInt(number.data.value);
}
let selector = csstree.generate(rule.ruleNode.prelude);
let counter;
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
}
let increment = {
selector: selector,
number: value || 1
};
counter.increments[selector] = increment;
increments.push(increment);
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
} }
let increment = { });
selector: selector,
number: number || 1
};
counter.increments[selector] = increment;
increments.push(increment);
}
return increments; return increments;
} }
handleReset(declaration, rule) { handleReset(declaration, rule) {
let names = [];
let values = [];
let resets = []; let resets = [];
declaration.value.children.forEach((child) => { let children = declaration.value.children;
if (child.type && child.type === "Identifier") {
names.push(child.name); children.forEach((data, item) => {
} if (data.type && data.type === "Identifier") {
if (child.type && child.type === "Number") { let name = data.name;
values.push(child.value); let whitespace, number, value;
} if (item.next && item.next.data.type === "WhiteSpace") {
}); whitespace = item.next;
for (const name of names) { }
let counter; if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
let selector; number = whitespace.next;
let prelude = rule.ruleNode.prelude; value = parseInt(number.data.value);
let number = values.shift(); }
if (rule.ruleNode.type === "Atrule" && rule.ruleNode.name === "page") {
selector = ".pagedjs_page";
} else {
selector = csstree.generate(prelude || rule.ruleNode);
}
if (name === "footnote") { let counter;
this.addFootnoteMarkerCounter(declaration.value.children); let selector;
} let prelude = rule.ruleNode.prelude;
if (!(name in this.counters)) { if (rule.ruleNode.type === "Atrule" && rule.ruleNode.name === "page") {
counter = this.addCounter(name); selector = ".pagedjs_page";
} else { } else {
counter = this.counters[name]; selector = csstree.generate(prelude || rule.ruleNode);
}
if (name === "footnote") {
this.addFootnoteMarkerCounter(declaration.value.children);
}
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
}
let reset = {
selector: selector,
number: value || 0
};
counter.resets[selector] = reset;
resets.push(reset);
if (selector !== ".pagedjs_page") {
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
}
} }
});
let reset = {
selector: selector,
number: number || 0
};
counter.resets[selector] = reset;
resets.push(reset);
}
return resets; return resets;
} }
...@@ -322,7 +363,9 @@ class Counters extends Handler { ...@@ -322,7 +363,9 @@ class Counters extends Handler {
resets.push(`footnote-marker ${value}`); resets.push(`footnote-marker ${value}`);
}); });
this.styleSheet.insertRule(`[data-page-number="${pageElement.dataset.pageNumber}"] { counter-increment: none; counter-reset: ${resets.join(" ")} }`, this.styleSheet.cssRules.length); if (resets.length) {
this.styleSheet.insertRule(`[data-page-number="${pageElement.dataset.pageNumber}"] { counter-increment: none; counter-reset: ${resets.join(" ")} }`, this.styleSheet.cssRules.length);
}
} }
} }
......
...@@ -36,7 +36,7 @@ class PageCounterIncrement extends Handler { ...@@ -36,7 +36,7 @@ class PageCounterIncrement extends Handler {
const number = declaration.value.children.getSize() > 1 ? declaration.value.children.last().value : 1; const number = declaration.value.children.getSize() > 1 ? declaration.value.children.last().value : 1;
const name = identifier && identifier.name; const name = identifier && identifier.name;
if (name.indexOf("target-counter-") === 0) { if (name && name.indexOf("target-counter-") === 0) {
return; return;
} }
// A counter named page is automatically created and incremented by 1 on every page of the document, // A counter named page is automatically created and incremented by 1 on every page of the document,
......
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