UNPKG

@magflip/scrollview

Version:

One of the core view plugins of MagFlip, displaying the book in a scrolling format.

179 lines (165 loc) 6.92 kB
/****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; /** * */ class ScrollView { constructor() { /** * This id is unique string for FlipView object. */ this.id = 'scroll-view'; ({ bookContainerEl: this.bookContainerEl } = this.createElements()); } /** * Creates the viewer related elements. * @returns ViewerElements */ createElements() { const bookContainerEl = document.createElement('div'); bookContainerEl.id = "bookContainer"; return { // bookViewerEl: bookViewerEl, bookContainerEl: bookContainerEl }; } ; getBookContainerEl() { return this.bookContainerEl; } zoom(zoomLevel) { } /** * * @param book * @param openPageIndex */ view(book, openPageIndex = 0) { this.attachBook(book); this.setViewer(); const indexRange = { start: openPageIndex - 3, cnt: 6 }; this.loadPages(indexRange); this.showPages(indexRange); return this.bookContainerEl; } /** * */ closeViewer() { // TODO Save current book status. this.detachBook(); } /** * Returns the book back to the BookManager. */ detachBook() { this.book = undefined; } /** * Attach a book to this book viewer. */ attachBook(book) { this.book = book; if (!(book === null || book === void 0 ? void 0 : book.element)) { throw new Error("Error the book opening"); } this.bookContainerEl.appendChild(book.element); } /** * Set the viewer to work. */ setViewer() { if (!this.book) { throw new Error("Book object does not exist."); } const { closed, opened } = this.book.size; const docStyle = document.documentElement.style; docStyle.setProperty('--opened-book-width', `${opened.width}px`); docStyle.setProperty('--closed-book-width', `${closed.width}px`); docStyle.setProperty('--book-height', `${closed.height}px`); docStyle.setProperty('--page-width', `${closed.width}px`); docStyle.setProperty('--page-height', `${closed.height}px`); } /** * Fetches and loads pages. * @param indexRange */ loadPages(indexRange) { return __awaiter(this, void 0, void 0, function* () { if (!this.book) { throw new Error("Error the book opening"); } return this.book.fetchPages(indexRange); }); } /** * * @param index */ showPages(indexRange) { const book = this.book; if (!book) { throw new Error("Error the book opening"); } // // Load the pages to the window // & append page elements to dom. // const maxIndex = indexRange.start + indexRange.cnt; for (let i = indexRange.start; i < maxIndex; i++) { const page = book.getPage(i); if (page) { book.appendPageEl(page.element); } } } nextPage(offsetY) { } prevPage(offsetY) { } moveTo(pageIndex, offsetY) { } } function styleInject(css, ref) { if ( ref === void 0 ) ref = {}; var insertAt = ref.insertAt; if (typeof document === 'undefined') { return; } var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (insertAt === 'top') { if (head.firstChild) { head.insertBefore(style, head.firstChild); } else { head.appendChild(style); } } else { head.appendChild(style); } if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } var css_248z = "#bookViewer.scroll-type.hidden{display:none}#bookViewer.scroll-view{align-items:center;background:hsla(0,0%,100%,.9);box-sizing:border-box;display:flex;height:100vh;justify-content:center;left:0;padding:0;position:fixed;top:0;width:100vw}.noselect{user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none}#bookViewer.scroll-view #bookContainer{align-items:center;display:flex;height:100%;justify-content:center;position:relative;width:100%}#bookViewer.scroll-view #bookContainer .book{align-items:center;box-sizing:content-box;display:flex;height:100%;justify-content:center;width:100%}#bookViewer.scroll-view .book>.container{background:transparent;height:100%;overflow-y:auto;position:relative;width:100%}#bookViewer.scroll-view .book>.container>.page.empty{opacity:0}#bookViewer.scroll-view .book>.container>.page{border:0;box-sizing:border-box;height:var(--page-height);margin:10px auto;position:relative;width:var(--page-width)}#bookViewer.scroll-view .book>.container>.page>.content-container{box-sizing:border-box;height:100%;left:0;overflow:hidden;position:absolute;top:0;width:100%}#bookViewer.scroll-view .book>.container>.page>.content-container>.content{height:100%;left:0;position:absolute;top:0;width:100%}img{height:100%;width:100%}"; styleInject(css_248z); export { ScrollView };