1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Add metatags for annotations

This commit is contained in:
Fred Chasen 2017-07-06 17:53:48 -04:00
parent fdf7fda0d9
commit 661d4d77d1
9 changed files with 111 additions and 50 deletions

View file

@ -8,15 +8,28 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script src="../dist/epub.js"></script> <script src="../dist/epub.js"></script>
<script> <!-- <script>
window.hypothesisConfig = function () { window.hypothesisConfig = function () {
return { return {
constructor: this.Annotator.Sidebar, // constructor: this.Annotator.Sidebar,
app: 'http://45.55.144.135:5000/app.html', app: 'https://hypothes.is/app.html',
};
};
</script> -->
<script type="text/javascript">
window.hypothesisConfig = function () {
return {
// Open the sidebar when the page loads
openSidebar: true,
// Needed for multi frame support
enableMultiFrameSupport: true,
embedScriptUrl: "https://cdn.hypothes.is/hypothesis"
}; };
}; };
</script> </script>
<script src="http://45.55.144.135:5000/embed.js"></script> <script src="https://cdn.hypothes.is/hypothesis"></script>
<style type="text/css"> <style type="text/css">
body { body {
@ -82,18 +95,13 @@
} }
#viewer { #viewer {
overflow: hidden; overflow: hidden;
width: 800px; width: 620px;
margin: 0 50px; margin: 0 50px;
background: url('ajax-loader.gif') center center no-repeat; /*background: url('ajax-loader.gif') center center no-repeat;*/
background-color: white;
}
#viewer .epub-view {
background: white;
box-shadow: 0 0 4px #ccc; box-shadow: 0 0 4px #ccc;
margin: 10px; margin: 20px;
padding: 40px 80px; padding: 40px 80px;
} }
@ -112,7 +120,7 @@
#pagination { #pagination {
text-align: center; text-align: center;
margin-left: 80px; margin: 20px;
/*padding: 0 50px;*/ /*padding: 0 50px;*/
} }
@ -179,10 +187,12 @@
</div> </div>
<script> <script>
var currentSectionIndex = 8;
// Load the opf // Load the opf
var book = ePub("https://s3.amazonaws.com/epubjs/books/alice/OPS/package.opf"); var book = ePub("https://s3.amazonaws.com/epubjs/books/alice/OPS/package.opf");
var rendition = book.renderTo("viewer"); var rendition = book.renderTo("viewer", {
flow: "scrolled-doc",
ignoreClass: "annotator-hl"
});
// var hash = window.location.hash.slice(2); // var hash = window.location.hash.slice(2);
var loc = window.location.href.indexOf("?loc="); var loc = window.location.href.indexOf("?loc=");
@ -190,7 +200,7 @@
var href = window.location.href.slice(loc + 5); var href = window.location.href.slice(loc + 5);
var hash = decodeURIComponent(href); var hash = decodeURIComponent(href);
} }
rendition.display(hash || 1); rendition.display(hash || undefined);
var next = document.getElementById("next"); var next = document.getElementById("next");
@ -316,30 +326,22 @@
}, 100); }, 100);
} }
book.rendition.hooks.content.register(function(view) { book.rendition.hooks.content.register(function(contents, view) {
view.window.hypothesisConfig = function () {
return {
constructor: this.Annotator.Guest,
app: 'http://45.55.144.135:5000/app.html'
};
};
view.contents.addScript("http://45.55.144.135:5000/embed.js").then(function() {
checkForAnnotator(function () { checkForAnnotator(function () {
var annotator = view.window.annotator; var annotator = contents.window.annotator;
annotator.on("scrollToAnnotation", function (node) { contents.window.addEventListener('scrolltorange', function (e) {
var cfi = view.section.cfiFromElement(node); var range = e.detail;
// var selectors = ev.target[0].selector; var cfi = new ePub.CFI(range, contents.cfiBase).toString();
// var range = selectors[0]; if (cfi) {
console.log("scrollToAnnotation", cfi);
book.rendition.display(cfi); book.rendition.display(cfi);
}
e.preventDefault();
}); });
annotator.on("highlightClick", function (annotation) { annotator.on("highlightClick", function (annotation) {
console.log(annotation); console.log(annotation);
window.annotator.show(); window.annotator.show();
@ -350,16 +352,17 @@
window.annotator.show(); window.annotator.show();
}) })
}, view.window); }, contents.window);
});
}); });
checkForAnnotator(function () { checkForAnnotator(function () {
var main = document.querySelector('#main'); var main = document.querySelector('#main');
var target = document.querySelector('.annotator-frame');
if(window.innerWidth < 1400) { if(window.innerWidth < 1400) {
/*
window.annotator.on("show", function () { window.annotator.on("show", function () {
main.classList.add("open"); main.classList.add("open");
}); });
@ -367,7 +370,25 @@
window.annotator.on("hide", function () { window.annotator.on("hide", function () {
main.classList.remove("open"); main.classList.remove("open");
}); });
*/
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === "class") {
if (target.classList.contains("annotator-collapsed")) {
main.classList.remove("open");
} else {
main.classList.add("open");
} }
}
});
});
observer.observe(target, { attributes: true });
}
}); });
</script> </script>

View file

@ -41,6 +41,10 @@
line-height: 1.33em; line-height: 1.33em;
color: #000; color: #000;
} }
#book-wrapper {
position: inherit;
}
</style> </style>

View file

@ -20,13 +20,14 @@
<script> <script>
var params = URLSearchParams && new URLSearchParams(document.location.search.substring(1)); var params = URLSearchParams && new URLSearchParams(document.location.search.substring(1));
var url = params && params.get("url") && decodeURIComponent(params.get("url")); var url = params && params.get("url") && decodeURIComponent(params.get("url"));
var currentSectionIndex = (params && params.get("loc")) ? params.get("loc") : 0; var currentSectionIndex = (params && params.get("loc")) ? params.get("loc") : 6;
// Load the opf // Load the opf
var book = ePub(url || "https://s3.amazonaws.com/moby-dick/moby-dick.epub"); var book = ePub(url || "https://s3.amazonaws.com/moby-dick/moby-dick.epub");
var rendition = book.renderTo("viewer", { var rendition = book.renderTo("viewer", {
width: "100%", width: "100%",
height: 600 height: 600,
spread: "always"
}); });
rendition.display(currentSectionIndex); rendition.display(currentSectionIndex);

View file

@ -11,7 +11,6 @@
} }
if (typeof module === 'object' && module.exports) { if (typeof module === 'object' && module.exports) {
// Node // Node
console.log("root", root);
module.exports = factory(root); module.exports = factory(root);
} else if (typeof define === 'function' && define.amd) { } else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module. // AMD. Register as an anonymous module.

View file

@ -182,7 +182,7 @@ class Layout {
*/ */
count(totalWidth) { count(totalWidth) {
// var totalWidth = contents.scrollWidth(); // var totalWidth = contents.scrollWidth();
var spreads = Math.ceil( totalWidth / this.spreadWidth); var spreads = Math.ceil( totalWidth / this.delta);
return { return {
spreads : spreads, spreads : spreads,

View file

@ -594,7 +594,6 @@ class DefaultViewManager {
this.scrollLeft = scrollLeft; this.scrollLeft = scrollLeft;
if(!this.ignore) { if(!this.ignore) {
console.log("scroll", scrollLeft, scrollTop);
this.emit("scroll", { this.emit("scroll", {
top: scrollTop, top: scrollTop,
left: scrollLeft left: scrollLeft

View file

@ -76,6 +76,8 @@ class Rendition {
this.hooks.content.register(this.passEvents.bind(this)); this.hooks.content.register(this.passEvents.bind(this));
this.hooks.content.register(this.adjustImages.bind(this)); this.hooks.content.register(this.adjustImages.bind(this));
this.book.spine.hooks.content.register(this.injectSource.bind(this));
if (this.settings.stylesheet) { if (this.settings.stylesheet) {
this.book.spine.hooks.content.register(this.injectStylesheet.bind(this)); this.book.spine.hooks.content.register(this.injectStylesheet.bind(this));
} }
@ -661,11 +663,13 @@ class Rendition {
} }
handleLinks(contents) { handleLinks(contents) {
if (contents) {
contents.on("link", (href) => { contents.on("link", (href) => {
let relative = this.book.path.relative(href); let relative = this.book.path.relative(href);
this.display(relative); this.display(relative);
}); });
} }
}
injectStylesheet(doc, section) { injectStylesheet(doc, section) {
let style = doc.createElement("link"); let style = doc.createElement("link");
@ -683,6 +687,16 @@ class Rendition {
doc.getElementsByTagName("head")[0].appendChild(script); doc.getElementsByTagName("head")[0].appendChild(script);
} }
injectSource(doc, section) {
let ident = this.book.package.metadata.identifier;
let meta = doc.createElement("meta");
meta.setAttribute("property", "dc:source");
if (ident) {
meta.setAttribute("contents", ident);
}
doc.getElementsByTagName("head")[0].appendChild(meta);
}
} }
//-- Enable binding events to Renderer //-- Enable binding events to Renderer

View file

@ -1,7 +1,7 @@
import EpubCFI from "./epubcfi"; import EpubCFI from "./epubcfi";
import Hook from "./utils/hook"; import Hook from "./utils/hook";
import Section from "./section"; import Section from "./section";
import {replaceBase, replaceCanonical} from "./utils/replacements"; import {replaceBase, replaceCanonical, replaceMeta} from "./utils/replacements";
/** /**
* A collection of Spine Items * A collection of Spine Items
@ -19,6 +19,7 @@ class Spine {
// Register replacements // Register replacements
this.hooks.content.register(replaceBase); this.hooks.content.register(replaceBase);
this.hooks.content.register(replaceCanonical); this.hooks.content.register(replaceCanonical);
this.hooks.content.register(replaceMeta);
this.epubcfi = new EpubCFI(); this.epubcfi = new EpubCFI();

View file

@ -44,6 +44,28 @@ export function replaceCanonical(doc, section){
head.appendChild(link); head.appendChild(link);
} }
} }
export function replaceMeta(doc, section){
var head;
var meta;
var id = section.idref;
if(!doc){
return;
}
head = qs(doc, "head");
meta = qs(head, "link[property='dc:identifier']");
if (meta) {
meta.setAttribute("content", id);
} else {
meta = doc.createElement("meta");
meta.setAttribute("property", "dc:identifier");
meta.setAttribute("content", id);
head.appendChild(meta);
}
}
// TODO: move me to Contents // TODO: move me to Contents
export function replaceLinks(contents, fn) { export function replaceLinks(contents, fn) {