From 81caaa2ebed066f80939b04f565b307ec0dd2d61 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 10 Sep 2020 16:12:09 +0300 Subject: Split web things into subdirectories --- web/frequently-used.js | 24 ----- web/index.css | 92 ------------------ web/index.html | 14 +-- web/index.js | 235 --------------------------------------------- web/index.sass | 116 ---------------------- web/package.json | 2 +- web/spinner.css | 64 ------------ web/spinner.js | 31 ------ web/src/frequently-used.js | 24 +++++ web/src/index.js | 235 +++++++++++++++++++++++++++++++++++++++++++++ web/src/spinner.js | 31 ++++++ web/src/widget-api.js | 66 +++++++++++++ web/style/index.css | 92 ++++++++++++++++++ web/style/index.sass | 116 ++++++++++++++++++++++ web/style/spinner.css | 64 ++++++++++++ web/widget-api.js | 66 ------------- 16 files changed, 636 insertions(+), 636 deletions(-) delete mode 100644 web/frequently-used.js delete mode 100644 web/index.css delete mode 100644 web/index.js delete mode 100644 web/index.sass delete mode 100644 web/spinner.css delete mode 100644 web/spinner.js create mode 100644 web/src/frequently-used.js create mode 100644 web/src/index.js create mode 100644 web/src/spinner.js create mode 100644 web/src/widget-api.js create mode 100644 web/style/index.css create mode 100644 web/style/index.sass create mode 100644 web/style/spinner.css delete mode 100644 web/widget-api.js diff --git a/web/frequently-used.js b/web/frequently-used.js deleted file mode 100644 index 1601e03..0000000 --- a/web/frequently-used.js +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2020 Tulir Asokan -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -const FREQUENTLY_USED = JSON.parse(window.localStorage.mauFrequentlyUsedStickerIDs || "{}") -let FREQUENTLY_USED_SORTED = null - -export const add = id => { - const [count] = FREQUENTLY_USED[id] || [0] - FREQUENTLY_USED[id] = [count + 1, Date.now()] - window.localStorage.mauFrequentlyUsedStickerIDs = JSON.stringify(FREQUENTLY_USED) - FREQUENTLY_USED_SORTED = null -} - -export const get = (limit = 16) => { - if (FREQUENTLY_USED_SORTED === null) { - FREQUENTLY_USED_SORTED = Object.entries(FREQUENTLY_USED) - .sort(([, [count1, date1]], [, [count2, date2]]) => - count2 === count1 ? date2 - date1 : count2 - count1) - .map(([emoji]) => emoji) - } - return FREQUENTLY_USED_SORTED.slice(0, limit) -} diff --git a/web/index.css b/web/index.css deleted file mode 100644 index 8877da6..0000000 --- a/web/index.css +++ /dev/null @@ -1,92 +0,0 @@ -* { - font-family: sans-serif; } - -body { - margin: 0; } - -h1 { - font-size: 1rem; } - -main.spinner { - margin-top: 5rem; } - -main.error, main.empty { - margin: 2rem; } - -main.empty { - text-align: center; } - -main.has-content { - position: fixed; - top: 0; - left: 0; - right: 0; - bottom: 0; - display: grid; - grid-template-rows: calc(12vw + 2px) auto; } - -nav { - display: flex; - overflow-x: auto; - height: calc(12vw + 2px); - background-color: white; } - nav > a { - border-bottom: 2px solid transparent; } - nav > a.visible { - border-bottom-color: green; } - nav > a > div.sticker { - width: 12vw; - height: 12vw; } - nav > a > div.sticker.icon > img { - width: 70%; - height: 70%; - padding: 15%; } - -div.pack-list, nav { - scrollbar-width: none; } - div.pack-list::-webkit-scrollbar, nav::-webkit-scrollbar { - display: none; } - -div.pack-list { - overflow-y: auto; } - -div.pack-list.ios-safari-hack { - position: fixed; - top: calc(12vw + 2px); - bottom: 0; - left: 0; - right: 0; - -webkit-overflow-scrolling: touch; } - -section.stickerpack { - margin-top: .75rem; } - section.stickerpack > div.sticker-list { - display: flex; - flex-wrap: wrap; } - section.stickerpack > h1 { - margin: 0 0 0 .75rem; } - -div.sticker { - display: flex; - padding: 4px; - cursor: pointer; - position: relative; - width: 25vw; - height: 25vw; - box-sizing: border-box; } - div.sticker:hover { - background-color: #eee; } - div.sticker > img { - display: none; - width: 100%; - object-fit: contain; } - div.sticker > img.visible { - display: initial; } - -div.settings-list { - display: flex; } - div.settings-list button { - width: 100%; - margin: .5rem; - padding: .5rem; - border-radius: .25rem; } diff --git a/web/index.html b/web/index.html index 1e84728..352f0b8 100644 --- a/web/index.html +++ b/web/index.html @@ -5,15 +5,15 @@ Maunium sticker picker - - - - + + + + - - - + + + diff --git a/web/index.js b/web/index.js deleted file mode 100644 index 342f524..0000000 --- a/web/index.js +++ /dev/null @@ -1,235 +0,0 @@ -// Copyright (c) 2020 Tulir Asokan -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -import { html, render, Component } from "./lib/htm/preact.js" -import { Spinner } from "./spinner.js" -import * as widgetAPI from "./widget-api.js" -import * as frequent from "./frequently-used.js" - -// The base URL for fetching packs. The app will first fetch ${PACK_BASE_URL}/index.json, -// then ${PACK_BASE_URL}/${packFile} for each packFile in the packs object of the index.json file. -const PACKS_BASE_URL = "packs" -// This is updated from packs/index.json -let HOMESERVER_URL = "https://matrix-client.matrix.org" - -const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${mxc.substr(6)}?height=128&width=128&method=scale` - -// We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs -// This is also used to fix scrolling to sections on Element iOS -const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) - -class App extends Component { - constructor(props) { - super(props) - this.state = { - packs: [], - loading: true, - error: null, - frequentlyUsed: { - id: "frequently-used", - title: "Frequently used", - stickerIDs: frequent.get(), - stickers: [], - }, - } - this.stickersByID = new Map(JSON.parse(localStorage.mauFrequentlyUsedStickerCache || "[]")) - this.state.frequentlyUsed.stickers = this._getStickersByID(this.state.frequentlyUsed.stickerIDs) - this.imageObserver = null - this.packListRef = null - this.navRef = null - this.sendSticker = this.sendSticker.bind(this) - this.navScroll = this.navScroll.bind(this) - this.reloadPacks = this.reloadPacks.bind(this) - } - - _getStickersByID(ids) { - return ids.map(id => this.stickersByID.get(id)).filter(sticker => !!sticker) - } - - updateFrequentlyUsed() { - const stickerIDs = frequent.get() - const stickers = this._getStickersByID(stickerIDs) - this.setState({ - frequentlyUsed: { - ...this.state.frequentlyUsed, - stickerIDs, - stickers, - }, - }) - localStorage.mauFrequentlyUsedStickerCache = JSON.stringify(stickers.map(sticker => [sticker.id, sticker])) - } - - reloadPacks() { - this.imageObserver.disconnect() - this.sectionObserver.disconnect() - this.setState({ packs: [] }) - this._loadPacks(true) - } - - _loadPacks(disableCache = false) { - const cache = disableCache ? "no-cache" : undefined - fetch(`${PACKS_BASE_URL}/index.json`, { cache }).then(async indexRes => { - if (indexRes.status >= 400) { - this.setState({ - loading: false, - error: indexRes.status !== 404 ? indexRes.statusText : null, - }) - return - } - const indexData = await indexRes.json() - HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL - // TODO only load pack metadata when scrolled into view? - for (const packFile of indexData.packs) { - const packRes = await fetch(`${PACKS_BASE_URL}/${packFile}`, { cache }) - const packData = await packRes.json() - for (const sticker of packData.stickers) { - this.stickersByID.set(sticker.id, sticker) - } - this.setState({ - packs: [...this.state.packs, packData], - loading: false, - }) - } - this.updateFrequentlyUsed() - }, error => this.setState({ loading: false, error })) - } - - componentDidMount() { - this._loadPacks() - this.imageObserver = new IntersectionObserver(this.observeImageIntersections, { - rootMargin: "100px", - }) - this.sectionObserver = new IntersectionObserver(this.observeSectionIntersections) - } - - observeImageIntersections(intersections) { - for (const entry of intersections) { - const img = entry.target.children.item(0) - if (entry.isIntersecting) { - img.setAttribute("src", img.getAttribute("data-src")) - img.classList.add("visible") - } else { - img.removeAttribute("src") - img.classList.remove("visible") - } - } - } - - observeSectionIntersections(intersections) { - for (const entry of intersections) { - const packID = entry.target.getAttribute("data-pack-id") - const navElement = document.getElementById(`nav-${packID}`) - if (entry.isIntersecting) { - navElement.classList.add("visible") - } else { - navElement.classList.remove("visible") - } - } - } - - componentDidUpdate() { - if (this.packListRef === null) { - return - } - for (const elem of this.packListRef.getElementsByClassName("sticker")) { - this.imageObserver.observe(elem) - } - for (const elem of this.packListRef.children) { - this.sectionObserver.observe(elem) - } - } - - componentWillUnmount() { - this.imageObserver.disconnect() - this.sectionObserver.disconnect() - } - - sendSticker(evt) { - const id = evt.currentTarget.getAttribute("data-sticker-id") - const sticker = this.stickersByID.get(id) - frequent.add(id) - this.updateFrequentlyUsed() - widgetAPI.sendSticker(sticker) - } - - navScroll(evt) { - this.navRef.scrollLeft += evt.deltaY * 12 - } - - render() { - if (this.state.loading) { - return html`
<${Spinner} size=${80} green />
` - } else if (this.state.error) { - return html`
-

Failed to load packs

-

${this.state.error}

-
` - } else if (this.state.packs.length === 0) { - return html`

No packs found 😿

` - } - return html`
- -
this.packListRef = elem}> - <${Pack} pack=${this.state.frequentlyUsed} send=${this.sendSticker} /> - ${this.state.packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)} - <${Settings} app=${this}/> -
-
` - } -} - -const Settings = ({ app }) => html` -
-

Settings

-
- -
-
-` - -// By default we just let the browser handle scrolling to sections, but webviews on Element iOS -// open the link in the browser instead of just scrolling there, so we need to scroll manually: -const scrollToSection = (evt, id) => { - const pack = document.getElementById(`pack-${id}`) - pack.scrollIntoView({ block: "start", behavior: "instant" }) - evt.preventDefault() -} - -const NavBarItem = ({ pack, iconOverride = null, altOverride = null }) => html` - scrollToSection(evt, pack.id)) : undefined}> -
- ${iconOverride ? html` - ${altOverride} - ` : html` - ${pack.stickers[0].body} - `} -
-
-` - -const Pack = ({ pack, send }) => html` -
-

${pack.title}

-
- ${pack.stickers.map(sticker => html` - <${Sticker} key=${sticker.id} content=${sticker} send=${send}/> - `)} -
-
-` - -const Sticker = ({ content, send }) => html` -
- ${content.body} -
-` - -render(html`<${App} />`, document.body) diff --git a/web/index.sass b/web/index.sass deleted file mode 100644 index d36e3f0..0000000 --- a/web/index.sass +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright (c) 2020 Tulir Asokan -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -* - font-family: sans-serif - -body - margin: 0 - -h1 - font-size: 1rem - -$sticker-size: 25vw -$nav-sticker-size: 12vw -$nav-bottom-highlight: 2px -$nav-height: calc(#{$nav-sticker-size} + #{$nav-bottom-highlight}) -$nav-height-inverse: calc(-#{$nav-sticker-size} - #{$nav-bottom-highlight}) - -main - &.spinner - margin-top: 5rem - - &.error, &.empty - margin: 2rem - - &.empty - text-align: center - - &.has-content - position: fixed - top: 0 - left: 0 - right: 0 - bottom: 0 - - display: grid - grid-template-rows: $nav-height auto - -nav - display: flex - overflow-x: auto - - height: $nav-height - - background-color: white - - > a - border-bottom: $nav-bottom-highlight solid transparent - &.visible - border-bottom-color: green - > div.sticker - width: $nav-sticker-size - height: $nav-sticker-size - > div.sticker.icon > img - width: 70% - height: 70% - padding: 15% - -div.pack-list, nav - scrollbar-width: none - - &::-webkit-scrollbar - display: none - -div.pack-list - overflow-y: auto - -div.pack-list.ios-safari-hack - position: fixed - top: $nav-height - bottom: 0 - left: 0 - right: 0 - -webkit-overflow-scrolling: touch - -section.stickerpack - margin-top: .75rem - - > div.sticker-list - display: flex - flex-wrap: wrap - - > h1 - margin: 0 0 0 .75rem - -div.sticker - display: flex - padding: 4px - cursor: pointer - position: relative - width: $sticker-size - height: $sticker-size - box-sizing: border-box - - &:hover - background-color: #eee - - > img - display: none - width: 100% - object-fit: contain - - &.visible - display: initial - -div.settings-list - display: flex - - button - width: 100% - margin: .5rem - padding: .5rem - border-radius: .25rem diff --git a/web/package.json b/web/package.json index 5926728..7f7f592 100644 --- a/web/package.json +++ b/web/package.json @@ -8,7 +8,7 @@ "private": true, "scripts": { "snowpack": "snowpack", - "sass": "node-sass -o . *.sass" + "sass": "node-sass -o style style/*.sass" }, "snowpack": { "install": [ diff --git a/web/spinner.css b/web/spinner.css deleted file mode 100644 index dcf6832..0000000 --- a/web/spinner.css +++ /dev/null @@ -1,64 +0,0 @@ -/* Chase spinner from https://tobiasahlin.com/spinkit/. MIT license */ -.sk-center-wrapper { - width: 100%; - display: flex; - justify-content: space-around; -} - -.sk-chase { - position: relative; - animation: sk-chase 2.5s infinite linear both; -} - -.sk-chase-dot { - width: 100%; - height: 100%; - position: absolute; - left: 0; - top: 0; - animation: sk-chase-dot 2.0s infinite ease-in-out both; -} - -.sk-chase-dot:before { - content: ''; - display: block; - width: 25%; - height: 25%; - border-radius: 100%; - animation: sk-chase-dot-before 2.0s infinite ease-in-out both; - background-color: #FFF; -} - -.sk-chase.green > .sk-chase-dot:before { - background-color: #00C853; -} - -.sk-chase-dot:nth-child(1) { animation-delay: -1.1s; } -.sk-chase-dot:nth-child(2) { animation-delay: -1.0s; } -.sk-chase-dot:nth-child(3) { animation-delay: -0.9s; } -.sk-chase-dot:nth-child(4) { animation-delay: -0.8s; } -.sk-chase-dot:nth-child(5) { animation-delay: -0.7s; } -.sk-chase-dot:nth-child(6) { animation-delay: -0.6s; } -.sk-chase-dot:nth-child(1):before { animation-delay: -1.1s; } -.sk-chase-dot:nth-child(2):before { animation-delay: -1.0s; } -.sk-chase-dot:nth-child(3):before { animation-delay: -0.9s; } -.sk-chase-dot:nth-child(4):before { animation-delay: -0.8s; } -.sk-chase-dot:nth-child(5):before { animation-delay: -0.7s; } -.sk-chase-dot:nth-child(6):before { animation-delay: -0.6s; } - -@keyframes sk-chase { - 100% { transform: rotate(360deg); } -} - -@keyframes sk-chase-dot { - 80%, 100% { transform: rotate(360deg); } -} - -@keyframes sk-chase-dot-before { - 50% { - transform: scale(0.4); - } - 100%, 0% { - transform: scale(1.0); - } -} diff --git a/web/spinner.js b/web/spinner.js deleted file mode 100644 index 6462cde..0000000 --- a/web/spinner.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2020 Tulir Asokan -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -import { html } from "https://unpkg.com/htm/preact/index.mjs?module" - -export const Spinner = ({ size = 40, noCenter = false, noMargin = false, green = false }) => { - let margin = 0 - if (!isNaN(+size)) { - size = +size - margin = noMargin ? 0 : `${Math.round(size / 6)}px` - size = `${size}px` - } - const noInnerMargin = !noCenter || !margin - const comp = html` -
-
-
-
-
-
-
-
- ` - if (!noCenter) { - return html`
${comp}
` - } - return comp -} diff --git a/web/src/frequently-used.js b/web/src/frequently-used.js new file mode 100644 index 0000000..1601e03 --- /dev/null +++ b/web/src/frequently-used.js @@ -0,0 +1,24 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +const FREQUENTLY_USED = JSON.parse(window.localStorage.mauFrequentlyUsedStickerIDs || "{}") +let FREQUENTLY_USED_SORTED = null + +export const add = id => { + const [count] = FREQUENTLY_USED[id] || [0] + FREQUENTLY_USED[id] = [count + 1, Date.now()] + window.localStorage.mauFrequentlyUsedStickerIDs = JSON.stringify(FREQUENTLY_USED) + FREQUENTLY_USED_SORTED = null +} + +export const get = (limit = 16) => { + if (FREQUENTLY_USED_SORTED === null) { + FREQUENTLY_USED_SORTED = Object.entries(FREQUENTLY_USED) + .sort(([, [count1, date1]], [, [count2, date2]]) => + count2 === count1 ? date2 - date1 : count2 - count1) + .map(([emoji]) => emoji) + } + return FREQUENTLY_USED_SORTED.slice(0, limit) +} diff --git a/web/src/index.js b/web/src/index.js new file mode 100644 index 0000000..815f321 --- /dev/null +++ b/web/src/index.js @@ -0,0 +1,235 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +import { html, render, Component } from "../lib/htm/preact.js" +import { Spinner } from "./spinner.js" +import * as widgetAPI from "./widget-api.js" +import * as frequent from "./frequently-used.js" + +// The base URL for fetching packs. The app will first fetch ${PACK_BASE_URL}/index.json, +// then ${PACK_BASE_URL}/${packFile} for each packFile in the packs object of the index.json file. +const PACKS_BASE_URL = "packs" +// This is updated from packs/index.json +let HOMESERVER_URL = "https://matrix-client.matrix.org" + +const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${mxc.substr(6)}?height=128&width=128&method=scale` + +// We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs +// This is also used to fix scrolling to sections on Element iOS +const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) + +class App extends Component { + constructor(props) { + super(props) + this.state = { + packs: [], + loading: true, + error: null, + frequentlyUsed: { + id: "frequently-used", + title: "Frequently used", + stickerIDs: frequent.get(), + stickers: [], + }, + } + this.stickersByID = new Map(JSON.parse(localStorage.mauFrequentlyUsedStickerCache || "[]")) + this.state.frequentlyUsed.stickers = this._getStickersByID(this.state.frequentlyUsed.stickerIDs) + this.imageObserver = null + this.packListRef = null + this.navRef = null + this.sendSticker = this.sendSticker.bind(this) + this.navScroll = this.navScroll.bind(this) + this.reloadPacks = this.reloadPacks.bind(this) + } + + _getStickersByID(ids) { + return ids.map(id => this.stickersByID.get(id)).filter(sticker => !!sticker) + } + + updateFrequentlyUsed() { + const stickerIDs = frequent.get() + const stickers = this._getStickersByID(stickerIDs) + this.setState({ + frequentlyUsed: { + ...this.state.frequentlyUsed, + stickerIDs, + stickers, + }, + }) + localStorage.mauFrequentlyUsedStickerCache = JSON.stringify(stickers.map(sticker => [sticker.id, sticker])) + } + + reloadPacks() { + this.imageObserver.disconnect() + this.sectionObserver.disconnect() + this.setState({ packs: [] }) + this._loadPacks(true) + } + + _loadPacks(disableCache = false) { + const cache = disableCache ? "no-cache" : undefined + fetch(`${PACKS_BASE_URL}/index.json`, { cache }).then(async indexRes => { + if (indexRes.status >= 400) { + this.setState({ + loading: false, + error: indexRes.status !== 404 ? indexRes.statusText : null, + }) + return + } + const indexData = await indexRes.json() + HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL + // TODO only load pack metadata when scrolled into view? + for (const packFile of indexData.packs) { + const packRes = await fetch(`${PACKS_BASE_URL}/${packFile}`, { cache }) + const packData = await packRes.json() + for (const sticker of packData.stickers) { + this.stickersByID.set(sticker.id, sticker) + } + this.setState({ + packs: [...this.state.packs, packData], + loading: false, + }) + } + this.updateFrequentlyUsed() + }, error => this.setState({ loading: false, error })) + } + + componentDidMount() { + this._loadPacks() + this.imageObserver = new IntersectionObserver(this.observeImageIntersections, { + rootMargin: "100px", + }) + this.sectionObserver = new IntersectionObserver(this.observeSectionIntersections) + } + + observeImageIntersections(intersections) { + for (const entry of intersections) { + const img = entry.target.children.item(0) + if (entry.isIntersecting) { + img.setAttribute("src", img.getAttribute("data-src")) + img.classList.add("visible") + } else { + img.removeAttribute("src") + img.classList.remove("visible") + } + } + } + + observeSectionIntersections(intersections) { + for (const entry of intersections) { + const packID = entry.target.getAttribute("data-pack-id") + const navElement = document.getElementById(`nav-${packID}`) + if (entry.isIntersecting) { + navElement.classList.add("visible") + } else { + navElement.classList.remove("visible") + } + } + } + + componentDidUpdate() { + if (this.packListRef === null) { + return + } + for (const elem of this.packListRef.getElementsByClassName("sticker")) { + this.imageObserver.observe(elem) + } + for (const elem of this.packListRef.children) { + this.sectionObserver.observe(elem) + } + } + + componentWillUnmount() { + this.imageObserver.disconnect() + this.sectionObserver.disconnect() + } + + sendSticker(evt) { + const id = evt.currentTarget.getAttribute("data-sticker-id") + const sticker = this.stickersByID.get(id) + frequent.add(id) + this.updateFrequentlyUsed() + widgetAPI.sendSticker(sticker) + } + + navScroll(evt) { + this.navRef.scrollLeft += evt.deltaY * 12 + } + + render() { + if (this.state.loading) { + return html`
<${Spinner} size=${80} green />
` + } else if (this.state.error) { + return html`
+

Failed to load packs

+

${this.state.error}

+
` + } else if (this.state.packs.length === 0) { + return html`

No packs found 😿

` + } + return html`
+ +
this.packListRef = elem}> + <${Pack} pack=${this.state.frequentlyUsed} send=${this.sendSticker} /> + ${this.state.packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)} + <${Settings} app=${this}/> +
+
` + } +} + +const Settings = ({ app }) => html` +
+

Settings

+
+ +
+
+` + +// By default we just let the browser handle scrolling to sections, but webviews on Element iOS +// open the link in the browser instead of just scrolling there, so we need to scroll manually: +const scrollToSection = (evt, id) => { + const pack = document.getElementById(`pack-${id}`) + pack.scrollIntoView({ block: "start", behavior: "instant" }) + evt.preventDefault() +} + +const NavBarItem = ({ pack, iconOverride = null, altOverride = null }) => html` + scrollToSection(evt, pack.id)) : undefined}> +
+ ${iconOverride ? html` + ${altOverride} + ` : html` + ${pack.stickers[0].body} + `} +
+
+` + +const Pack = ({ pack, send }) => html` +
+

${pack.title}

+
+ ${pack.stickers.map(sticker => html` + <${Sticker} key=${sticker.id} content=${sticker} send=${send}/> + `)} +
+
+` + +const Sticker = ({ content, send }) => html` +
+ ${content.body} +
+` + +render(html`<${App} />`, document.body) diff --git a/web/src/spinner.js b/web/src/spinner.js new file mode 100644 index 0000000..b44740e --- /dev/null +++ b/web/src/spinner.js @@ -0,0 +1,31 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +import { html } from "../lib/htm/preact.js" + +export const Spinner = ({ size = 40, noCenter = false, noMargin = false, green = false }) => { + let margin = 0 + if (!isNaN(+size)) { + size = +size + margin = noMargin ? 0 : `${Math.round(size / 6)}px` + size = `${size}px` + } + const noInnerMargin = !noCenter || !margin + const comp = html` +
+
+
+
+
+
+
+
+ ` + if (!noCenter) { + return html`
${comp}
` + } + return comp +} diff --git a/web/src/widget-api.js b/web/src/widget-api.js new file mode 100644 index 0000000..3efca0e --- /dev/null +++ b/web/src/widget-api.js @@ -0,0 +1,66 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +let widgetId = null + +window.onmessage = event => { + if (!window.parent || !event.data) { + return + } + + const request = event.data + if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") { + return + } + + if (widgetId) { + if (widgetId !== request.widgetId) { + return + } + } else { + widgetId = request.widgetId + } + + let response + + if (request.action === "visibility") { + response = {} + } else if (request.action === "capabilities") { + response = { capabilities: ["m.sticker"] } + } else { + response = { error: { message: "Action not supported" } } + } + + window.parent.postMessage({ ...request, response }, event.origin) +} + +export function sendSticker(content) { + const data = { + content: { ...content }, + // `name` is for Element Web (and also the spec) + // Element Android uses content -> body as the name + name: content.body, + } + // Custom field that stores the ID even for non-telegram stickers + delete data.content.id + + // This is for Element iOS + const widgetData = { + ...data, + description: content.body, + file: `${content.id}.png`, + } + // Element iOS explodes if there are extra fields present + delete widgetData.content["net.maunium.telegram.sticker"] + + window.parent.postMessage({ + api: "fromWidget", + action: "m.sticker", + requestId: `sticker-${Date.now()}`, + widgetId, + data, + widgetData, + }, "*") +} diff --git a/web/style/index.css b/web/style/index.css new file mode 100644 index 0000000..8877da6 --- /dev/null +++ b/web/style/index.css @@ -0,0 +1,92 @@ +* { + font-family: sans-serif; } + +body { + margin: 0; } + +h1 { + font-size: 1rem; } + +main.spinner { + margin-top: 5rem; } + +main.error, main.empty { + margin: 2rem; } + +main.empty { + text-align: center; } + +main.has-content { + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + display: grid; + grid-template-rows: calc(12vw + 2px) auto; } + +nav { + display: flex; + overflow-x: auto; + height: calc(12vw + 2px); + background-color: white; } + nav > a { + border-bottom: 2px solid transparent; } + nav > a.visible { + border-bottom-color: green; } + nav > a > div.sticker { + width: 12vw; + height: 12vw; } + nav > a > div.sticker.icon > img { + width: 70%; + height: 70%; + padding: 15%; } + +div.pack-list, nav { + scrollbar-width: none; } + div.pack-list::-webkit-scrollbar, nav::-webkit-scrollbar { + display: none; } + +div.pack-list { + overflow-y: auto; } + +div.pack-list.ios-safari-hack { + position: fixed; + top: calc(12vw + 2px); + bottom: 0; + left: 0; + right: 0; + -webkit-overflow-scrolling: touch; } + +section.stickerpack { + margin-top: .75rem; } + section.stickerpack > div.sticker-list { + display: flex; + flex-wrap: wrap; } + section.stickerpack > h1 { + margin: 0 0 0 .75rem; } + +div.sticker { + display: flex; + padding: 4px; + cursor: pointer; + position: relative; + width: 25vw; + height: 25vw; + box-sizing: border-box; } + div.sticker:hover { + background-color: #eee; } + div.sticker > img { + display: none; + width: 100%; + object-fit: contain; } + div.sticker > img.visible { + display: initial; } + +div.settings-list { + display: flex; } + div.settings-list button { + width: 100%; + margin: .5rem; + padding: .5rem; + border-radius: .25rem; } diff --git a/web/style/index.sass b/web/style/index.sass new file mode 100644 index 0000000..d36e3f0 --- /dev/null +++ b/web/style/index.sass @@ -0,0 +1,116 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +* + font-family: sans-serif + +body + margin: 0 + +h1 + font-size: 1rem + +$sticker-size: 25vw +$nav-sticker-size: 12vw +$nav-bottom-highlight: 2px +$nav-height: calc(#{$nav-sticker-size} + #{$nav-bottom-highlight}) +$nav-height-inverse: calc(-#{$nav-sticker-size} - #{$nav-bottom-highlight}) + +main + &.spinner + margin-top: 5rem + + &.error, &.empty + margin: 2rem + + &.empty + text-align: center + + &.has-content + position: fixed + top: 0 + left: 0 + right: 0 + bottom: 0 + + display: grid + grid-template-rows: $nav-height auto + +nav + display: flex + overflow-x: auto + + height: $nav-height + + background-color: white + + > a + border-bottom: $nav-bottom-highlight solid transparent + &.visible + border-bottom-color: green + > div.sticker + width: $nav-sticker-size + height: $nav-sticker-size + > div.sticker.icon > img + width: 70% + height: 70% + padding: 15% + +div.pack-list, nav + scrollbar-width: none + + &::-webkit-scrollbar + display: none + +div.pack-list + overflow-y: auto + +div.pack-list.ios-safari-hack + position: fixed + top: $nav-height + bottom: 0 + left: 0 + right: 0 + -webkit-overflow-scrolling: touch + +section.stickerpack + margin-top: .75rem + + > div.sticker-list + display: flex + flex-wrap: wrap + + > h1 + margin: 0 0 0 .75rem + +div.sticker + display: flex + padding: 4px + cursor: pointer + position: relative + width: $sticker-size + height: $sticker-size + box-sizing: border-box + + &:hover + background-color: #eee + + > img + display: none + width: 100% + object-fit: contain + + &.visible + display: initial + +div.settings-list + display: flex + + button + width: 100% + margin: .5rem + padding: .5rem + border-radius: .25rem diff --git a/web/style/spinner.css b/web/style/spinner.css new file mode 100644 index 0000000..dcf6832 --- /dev/null +++ b/web/style/spinner.css @@ -0,0 +1,64 @@ +/* Chase spinner from https://tobiasahlin.com/spinkit/. MIT license */ +.sk-center-wrapper { + width: 100%; + display: flex; + justify-content: space-around; +} + +.sk-chase { + position: relative; + animation: sk-chase 2.5s infinite linear both; +} + +.sk-chase-dot { + width: 100%; + height: 100%; + position: absolute; + left: 0; + top: 0; + animation: sk-chase-dot 2.0s infinite ease-in-out both; +} + +.sk-chase-dot:before { + content: ''; + display: block; + width: 25%; + height: 25%; + border-radius: 100%; + animation: sk-chase-dot-before 2.0s infinite ease-in-out both; + background-color: #FFF; +} + +.sk-chase.green > .sk-chase-dot:before { + background-color: #00C853; +} + +.sk-chase-dot:nth-child(1) { animation-delay: -1.1s; } +.sk-chase-dot:nth-child(2) { animation-delay: -1.0s; } +.sk-chase-dot:nth-child(3) { animation-delay: -0.9s; } +.sk-chase-dot:nth-child(4) { animation-delay: -0.8s; } +.sk-chase-dot:nth-child(5) { animation-delay: -0.7s; } +.sk-chase-dot:nth-child(6) { animation-delay: -0.6s; } +.sk-chase-dot:nth-child(1):before { animation-delay: -1.1s; } +.sk-chase-dot:nth-child(2):before { animation-delay: -1.0s; } +.sk-chase-dot:nth-child(3):before { animation-delay: -0.9s; } +.sk-chase-dot:nth-child(4):before { animation-delay: -0.8s; } +.sk-chase-dot:nth-child(5):before { animation-delay: -0.7s; } +.sk-chase-dot:nth-child(6):before { animation-delay: -0.6s; } + +@keyframes sk-chase { + 100% { transform: rotate(360deg); } +} + +@keyframes sk-chase-dot { + 80%, 100% { transform: rotate(360deg); } +} + +@keyframes sk-chase-dot-before { + 50% { + transform: scale(0.4); + } + 100%, 0% { + transform: scale(1.0); + } +} diff --git a/web/widget-api.js b/web/widget-api.js deleted file mode 100644 index 3efca0e..0000000 --- a/web/widget-api.js +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2020 Tulir Asokan -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -let widgetId = null - -window.onmessage = event => { - if (!window.parent || !event.data) { - return - } - - const request = event.data - if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") { - return - } - - if (widgetId) { - if (widgetId !== request.widgetId) { - return - } - } else { - widgetId = request.widgetId - } - - let response - - if (request.action === "visibility") { - response = {} - } else if (request.action === "capabilities") { - response = { capabilities: ["m.sticker"] } - } else { - response = { error: { message: "Action not supported" } } - } - - window.parent.postMessage({ ...request, response }, event.origin) -} - -export function sendSticker(content) { - const data = { - content: { ...content }, - // `name` is for Element Web (and also the spec) - // Element Android uses content -> body as the name - name: content.body, - } - // Custom field that stores the ID even for non-telegram stickers - delete data.content.id - - // This is for Element iOS - const widgetData = { - ...data, - description: content.body, - file: `${content.id}.png`, - } - // Element iOS explodes if there are extra fields present - delete widgetData.content["net.maunium.telegram.sticker"] - - window.parent.postMessage({ - api: "fromWidget", - action: "m.sticker", - requestId: `sticker-${Date.now()}`, - widgetId, - data, - widgetData, - }, "*") -} -- cgit v1.2.3