Commit b4c54d1b authored by Fred Chasen's avatar Fred Chasen

Update target counters to use any counter, page counter fixes

parent cfe3f992
......@@ -29,11 +29,21 @@ class TargetCounters extends Handler {
});
let counter;
if (last !== first) {
counter = last.name;
}
let style;
let styleIdentifier;
funcNode.children.forEach((child) => {
if (child.type === "Identifier") {
if (!counter) {
counter = child.name;
} else if (!style) {
styleIdentifier = csstree.clone(child);
style = child.name;
}
}
})
let variable = "--" + UUID();
let variable = "target-counter-" + UUID();
selector.split(",").forEach((s) => {
this.counterTargets[s] = {
......@@ -41,20 +51,25 @@ class TargetCounters extends Handler {
args: args,
value: value,
counter: counter,
style: style,
selector: s,
fullSelector: selector,
variable: variable
}
});
// Replace with variable
funcNode.name = "var";
// Replace with counter
funcNode.name = "counter";
funcNode.children = new csstree.List()
funcNode.children.appendData({
type: "Identifier",
loc: 0,
name: variable
});
if (styleIdentifier) {
funcNode.children.appendData({type: "Operator", loc: null, value: ","});
funcNode.children.appendData(styleIdentifier);
}
}
}
......@@ -63,7 +78,8 @@ class TargetCounters extends Handler {
let target = this.counterTargets[name];
let split = target.selector.split("::");
let query = split[0];
let queried = chunker.pagesArea.querySelectorAll(query + ":not([data-target-counter])");
let queried = chunker.pagesArea.querySelectorAll(query + ":not([data-" + target.variable + "])");
queried.forEach((selected, index) => {
// TODO: handle func other than attr
......@@ -75,13 +91,20 @@ class TargetCounters extends Handler {
if (element) {
let selector = UUID();
selected.setAttribute("data-target-counter", selector);
selected.setAttribute("data-" + target.variable, selector);
// TODO: handle other counter types (by query)
if (target.counter === "page") {
let pages = chunker.pagesArea.querySelectorAll(".pagedjs_page");
let pg = 0;
for (var i = 0; i < pages.length; i++) {
let styles = window.getComputedStyle(pages[i]);
let reset = styles["counter-reset"].replace("page", "").trim();
if (reset !== "none") {
pg = parseInt(reset);
}
pg += 1;
if (pages[i].contains( element )){
break;
}
......@@ -92,8 +115,12 @@ class TargetCounters extends Handler {
psuedo += "::" + split[1];
}
// this.styleSheet.insertRule(`[data-target-counter="${selector}"]${psuedo} { content: "${pg}"; }`, this.styleSheet.cssRules.length);
this.styleSheet.insertRule(`[data-target-counter="${selector}"]${psuedo} { ${target.variable}: "${pg}" }`, this.styleSheet.cssRules.length);
this.styleSheet.insertRule(`[data-${target.variable}="${selector}"] { counter-increment: ${target.variable} ${pg}; }`, this.styleSheet.cssRules.length);
} else {
let value = element.getAttribute(`data-counter-${target.counter}-value`);
if (value) {
this.styleSheet.insertRule(`[data-${target.variable}="${selector}"] { counter-increment: ${target.variable} ${parseInt(value)}; }`, this.styleSheet.cssRules.length);
}
}
}
});
......
......@@ -790,13 +790,14 @@ class AtPage extends Handler {
addPageAttributes(page, start, pages) {
let named = start.dataset.page;
let index = pages.indexOf(page);
if (named) {
page.name = named;
page.element.classList.add("pagedjs_named_page");
page.element.classList.add("pagedjs_" + named + "_page");
if (!start.dataset.splitFrom) {
if (index === 0 || pages[index-1].name !== named) {
page.element.classList.add("pagedjs_" + named + "_first_page");
}
}
......
......@@ -14,11 +14,15 @@ class Counters extends Handler {
let property = declaration.property;
if (property === "counter-increment") {
this.handleIncrement(declaration, rule);
dList.remove(dItem);
let inc = this.handleIncrement(declaration, rule);
if (inc) {
dList.remove(dItem);
}
} else if (property === "counter-reset") {
this.handleReset(declaration, rule);
dList.remove(dItem);
let reset = this.handleReset(declaration, rule);
if (reset) {
dList.remove(dItem);
}
}
}
......@@ -51,6 +55,11 @@ class Counters extends Handler {
let number = declaration.value.children.getSize() > 1
&& declaration.value.children.last().value;
let name = identifier && identifier.name;
if (name === "page" || name.indexOf("target-counter-") === 0) {
return;
}
let selector = csstree.generate(rule.ruleNode.prelude);
let counter;
......@@ -60,7 +69,7 @@ class Counters extends Handler {
counter = this.counters[name];
}
counter.increments[selector] = {
return counter.increments[selector] = {
selector: selector,
number: number || 1
};
......@@ -72,15 +81,17 @@ class Counters extends Handler {
&& declaration.value.children.last().value;
let name = identifier && identifier.name;
let selector = csstree.generate(rule.ruleNode.prelude);
let counter;
if (name === "page") {
return;
}
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
}
counter.resets[selector] = {
return counter.resets[selector] = {
selector: selector,
number: number || 0
};
......
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