This commit is contained in:
salixor 2023-09-13 20:31:41 +02:00 committed by GitHub
commit 07aff06fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 204 additions and 61 deletions

1
web/res/reset.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/></svg>

After

Width:  |  Height:  |  Size: 313 B

View File

@ -15,7 +15,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { html, render, Component } from "../lib/htm/preact.js" import { html, render, Component } from "../lib/htm/preact.js"
import { Spinner } from "./spinner.js" import { Spinner } from "./spinner.js"
import { SearchBox } from "./search-box.js" import { shouldAutofocusSearchBar, shouldDisplayAutofocusSearchBar, SearchBox } from "./search-box.js"
import { checkMobileSafari } from "./user-agent-detect.js"
import * as widgetAPI from "./widget-api.js" import * as widgetAPI from "./widget-api.js"
import * as frequent from "./frequently-used.js" import * as frequent from "./frequently-used.js"
@ -35,16 +36,24 @@ const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${
// We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs // 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 // This is also used to fix scrolling to sections on Element iOS
const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) const isMobileSafari = checkMobileSafari()
// We need to detect iOS webkit / Android because autofocusing a field does not open
// the device keyboard by design, making the option obsolete
const shouldAutofocusOption = shouldAutofocusSearchBar()
const displayAutofocusOption = shouldDisplayAutofocusSearchBar()
const supportedThemes = ["light", "dark", "black"] const supportedThemes = ["light", "dark", "black"]
const defaultState = { const defaultState = {
packs: [], packs: [],
filtering: { loading: true,
searchTerm: "", error: null,
packs: [], }
},
const defaultSearchState = {
searchTerm: null,
filteredPacks: null
} }
class App extends Component { class App extends Component {
@ -52,9 +61,8 @@ class App extends Component {
super(props) super(props)
this.defaultTheme = params.get("theme") this.defaultTheme = params.get("theme")
this.state = { this.state = {
packs: defaultState.packs, ...defaultState,
loading: true, ...defaultSearchState,
error: null,
stickersPerRow: parseInt(localStorage.mauStickersPerRow || "4"), stickersPerRow: parseInt(localStorage.mauStickersPerRow || "4"),
theme: localStorage.mauStickerThemeOverride || this.defaultTheme, theme: localStorage.mauStickerThemeOverride || this.defaultTheme,
frequentlyUsed: { frequentlyUsed: {
@ -63,7 +71,6 @@ class App extends Component {
stickerIDs: frequent.get(), stickerIDs: frequent.get(),
stickers: [], stickers: [],
}, },
filtering: defaultState.filtering,
} }
if (!supportedThemes.includes(this.state.theme)) { if (!supportedThemes.includes(this.state.theme)) {
this.state.theme = "light" this.state.theme = "light"
@ -77,6 +84,7 @@ class App extends Component {
this.packListRef = null this.packListRef = null
this.navRef = null this.navRef = null
this.searchStickers = this.searchStickers.bind(this) this.searchStickers = this.searchStickers.bind(this)
this.resetSearch = this.resetSearch.bind(this)
this.sendSticker = this.sendSticker.bind(this) this.sendSticker = this.sendSticker.bind(this)
this.navScroll = this.navScroll.bind(this) this.navScroll = this.navScroll.bind(this)
this.reloadPacks = this.reloadPacks.bind(this) this.reloadPacks = this.reloadPacks.bind(this)
@ -101,28 +109,33 @@ class App extends Component {
localStorage.mauFrequentlyUsedStickerCache = JSON.stringify(stickers.map(sticker => [sticker.id, sticker])) localStorage.mauFrequentlyUsedStickerCache = JSON.stringify(stickers.map(sticker => [sticker.id, sticker]))
} }
searchStickers(e) { // Search
resetSearch() {
this.setState({ ...defaultSearchState })
}
searchStickers(searchTerm) {
const sanitizeString = s => s.toLowerCase().trim() const sanitizeString = s => s.toLowerCase().trim()
const searchTerm = sanitizeString(e.target.value) const sanitizedSearch = sanitizeString(searchTerm)
const allPacks = [this.state.frequentlyUsed, ...this.state.packs] const allPacks = [this.state.frequentlyUsed, ...this.state.packs]
const packsWithFilteredStickers = allPacks.map(pack => ({ const packsWithFilteredStickers = allPacks.map(pack => ({
...pack, ...pack,
stickers: pack.stickers.filter(sticker => stickers: pack.stickers.filter(sticker =>
sanitizeString(sticker.body).includes(searchTerm) || sanitizeString(sticker.body).includes(sanitizedSearch) ||
sanitizeString(sticker.id).includes(searchTerm) sanitizeString(sticker.id).includes(sanitizedSearch)
), ),
})) }))
const filteredPacks = packsWithFilteredStickers.filter(({ stickers }) => !!stickers.length)
this.setState({ this.setState({ searchTerm, filteredPacks })
filtering: {
...this.state.filtering,
searchTerm,
packs: packsWithFilteredStickers.filter(({ stickers }) => !!stickers.length),
},
})
} }
// End search
// Settings
setStickersPerRow(val) { setStickersPerRow(val) {
localStorage.mauStickersPerRow = val localStorage.mauStickersPerRow = val
document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow) document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow)
@ -142,13 +155,17 @@ class App extends Component {
} }
} }
setAutofocusSearchBar(checked) {
localStorage.mauAutofocusSearchBar = checked
}
// End settings
reloadPacks() { reloadPacks() {
this.imageObserver.disconnect() this.imageObserver.disconnect()
this.sectionObserver.disconnect() this.sectionObserver.disconnect()
this.setState({ this.setState({ packs: defaultState.packs })
packs: defaultState.packs, this.resetSearch()
filtering: defaultState.filtering,
})
this._loadPacks(true) this._loadPacks(true)
} }
@ -215,6 +232,9 @@ class App extends Component {
for (const entry of intersections) { for (const entry of intersections) {
const packID = entry.target.getAttribute("data-pack-id") const packID = entry.target.getAttribute("data-pack-id")
const navElement = document.getElementById(`nav-${packID}`) const navElement = document.getElementById(`nav-${packID}`)
if (!navElement) {
continue
}
if (entry.isIntersecting) { if (entry.isIntersecting) {
navElement.classList.add("visible") navElement.classList.add("visible")
const bb = navElement.getBoundingClientRect() const bb = navElement.getBoundingClientRect()
@ -258,6 +278,7 @@ class App extends Component {
const sticker = this.stickersByID.get(id) const sticker = this.stickersByID.get(id)
frequent.add(id) frequent.add(id)
this.updateFrequentlyUsed() this.updateFrequentlyUsed()
this.resetSearch()
widgetAPI.sendSticker(sticker) widgetAPI.sendSticker(sticker)
} }
@ -267,8 +288,10 @@ class App extends Component {
render() { render() {
const theme = `theme-${this.state.theme}` const theme = `theme-${this.state.theme}`
const filterActive = !!this.state.filtering.searchTerm
const packs = filterActive ? this.state.filtering.packs : [this.state.frequentlyUsed, ...this.state.packs] const filterActive = !!this.state.filteredPacks
const packs = filterActive ? this.state.filteredPacks : [this.state.frequentlyUsed, ...this.state.packs]
const noPacksForSearch = filterActive && packs.length === 0
if (this.state.loading) { if (this.state.loading) {
return html`<main class="spinner ${theme}"><${Spinner} size=${80} green /></main>` return html`<main class="spinner ${theme}"><${Spinner} size=${80} green /></main>`
@ -287,9 +310,13 @@ class App extends Component {
${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)} ${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)}
<${NavBarItem} pack=${{ id: "settings", title: "Settings" }} iconOverride="settings" /> <${NavBarItem} pack=${{ id: "settings", title: "Settings" }} iconOverride="settings" />
</nav> </nav>
<${SearchBox} onKeyUp=${this.searchStickers} /> <${SearchBox}
value=${this.state.searchTerm}
onSearch=${this.searchStickers}
onReset=${this.resetSearch}
/>
<div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${elem => this.packListRef = elem}> <div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${elem => this.packListRef = elem}>
${filterActive && packs.length === 0 ? html`<div class="search-empty"><h1>No stickers match your search</h1></div>` : null} ${noPacksForSearch ? html`<div class="search-empty"><h1>No stickers match your search</h1></div>` : null}
${packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)} ${packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)}
<${Settings} app=${this}/> <${Settings} app=${this}/>
</div> </div>
@ -317,6 +344,14 @@ const Settings = ({ app }) => html`
<option value="black">Black</option> <option value="black">Black</option>
</select> </select>
</div> </div>
${displayAutofocusOption ? html`<div>
Autofocus search bar:
<input
type="checkbox"
checked=${shouldAutofocusOption}
onChange=${evt => app.setAutofocusSearchBar(evt.target.checked)}
/>
</div>` : null}
</div> </div>
</section> </section>
` `

View File

@ -13,14 +13,77 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { html } from "../lib/htm/preact.js" import { html, Component } from "../lib/htm/preact.js"
import { checkMobileSafari, checkAndroid } from "./user-agent-detect.js"
export const SearchBox = ({ onKeyUp, placeholder = 'Find stickers' }) => { export function shouldDisplayAutofocusSearchBar() {
const component = html` return !checkMobileSafari() && !checkAndroid()
<div class="search-box"> }
<input type="text" placeholder=${placeholder} onKeyUp=${onKeyUp} />
<span class="icon icon-search" /> export function shouldAutofocusSearchBar() {
</div> return localStorage.mauAutofocusSearchBar === 'true' && shouldDisplayAutofocusSearchBar()
` }
return component
export function focusSearchBar() {
const inputInWebView = document.querySelector('.search-box input')
if (inputInWebView && shouldAutofocusSearchBar()) {
inputInWebView.focus()
}
}
export class SearchBox extends Component {
constructor(props) {
super(props)
this.autofocus = shouldAutofocusSearchBar()
this.value = props.value
this.onSearch = props.onSearch
this.onReset = props.onReset
this.search = this.search.bind(this)
this.clearSearch = this.clearSearch.bind(this)
}
componentDidMount() {
focusSearchBar()
}
componentWillReceiveProps(props) {
this.value = props.value
}
search(e) {
if (e.key === "Escape") {
this.clearSearch()
return
}
this.onSearch(e.target.value)
}
clearSearch() {
this.onReset()
}
render() {
const isEmpty = !this.value
const className = `icon-display ${isEmpty ? null : 'reset-click-zone'}`
const title = isEmpty ? null : 'Click to reset'
const onClick = isEmpty ? null : this.clearSearch
const iconToDisplay = `icon-${isEmpty ? 'search' : 'reset'}`
return html`
<div class="search-box">
<input
placeholder="Find stickers …"
value=${this.value}
onKeyUp=${this.search}
autoFocus=${this.autofocus}
/>
<div class=${className} title=${title} onClick=${onClick}>
<span class="icon ${iconToDisplay}" />
</div>
</div>
`
}
} }

View File

@ -0,0 +1,18 @@
export const getUserAgent = () => navigator.userAgent || navigator.vendor || window.opera
export const checkiOSDevice = () => {
const agent = getUserAgent()
return agent.match(/(iPod|iPhone|iPad)/)
}
export const checkMobileSafari = () => {
const agent = getUserAgent()
return agent.match(/(iPod|iPhone|iPad)/) && agent.match(/AppleWebKit/)
}
export const checkAndroid = () => {
const agent = getUserAgent()
return agent.match(/android/i)
}
export const checkMobileDevice = () => checkiOSDevice() || checkAndroid()

View File

@ -13,6 +13,8 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { focusSearchBar } from "./search-box.js"
let widgetId = null let widgetId = null
window.onmessage = event => { window.onmessage = event => {
@ -33,10 +35,12 @@ window.onmessage = event => {
widgetId = request.widgetId widgetId = request.widgetId
} }
let response let response = {}
if (request.action === "visibility") { if (request.action === "visibility") { // visibility of the widget changed
response = {} if (request.visible) {
focusSearchBar() // we have to re-focus the search bar when appropriate
}
} else if (request.action === "capabilities") { } else if (request.action === "capabilities") {
response = { capabilities: ["m.sticker"] } response = { capabilities: ["m.sticker"] }
} else { } else {

View File

@ -1 +1 @@
*{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main{color:var(--text-color)}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) min-content auto}main.theme-light{--highlight-color: #eee;--search-box-color: var(--highlight-color);--text-color: black;background-color:#fff}main.theme-dark{--highlight-color: #444;--search-box-color: #383e4b;--text-color: white;background-color:#22262e}main.theme-black{--highlight-color: #222;--search-box-color: var(--highlight-color);--text-color: white;background-color:#000}.icon{width:100%;height:100%;background-color:var(--text-color);mask-size:contain;-webkit-mask-size:contain;mask-image:var(--icon-image);-webkit-mask-image:var(--icon-image)}.icon.icon-settings{--icon-image: url(../res/settings.svg)}.icon.icon-recent{--icon-image: url(../res/recent.svg)}.icon.icon.icon-search{--icon-image: url(../res/search.svg)}nav{display:flex;overflow-x:auto}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}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(calc(12vw + 2px) + calc(2 * 0.7rem + 2 * 0.5rem + 1rem));bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}div.search-empty{margin:1.2rem;text-align:center}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:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:var(--highlight-color)}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.sticker>.icon{width:70%;height:70%;margin:15%}div.search-box{position:relative;display:flex}div.search-box>input[type=text]{flex-grow:1;background-color:var(--search-box-color);outline:none;border:none;border-radius:.25rem;height:1rem;padding:.7rem;padding-right:calc(1rem + 0.7rem);margin:.5rem;font-size:1rem;color:var(--text-color)}div.search-box>span.icon{display:flex;position:absolute;top:calc(50% - 1rem / 2);right:1rem;width:1rem;height:1rem;box-sizing:border-box}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input{width:100%} *{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main{color:var(--text-color)}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) min-content auto}main.theme-light{--highlight-color: #eee;--search-box-color: var(--highlight-color);--text-color: black;background-color:#fff}main.theme-dark{--highlight-color: #444;--search-box-color: #383e4b;--text-color: white;background-color:#22262e}main.theme-black{--highlight-color: #222;--search-box-color: var(--highlight-color);--text-color: white;background-color:#000}.icon{width:100%;height:100%;background-color:var(--text-color);mask-size:contain;-webkit-mask-size:contain;mask-image:var(--icon-image);-webkit-mask-image:var(--icon-image)}.icon.icon-settings{--icon-image: url(../res/settings.svg)}.icon.icon-recent{--icon-image: url(../res/recent.svg)}.icon.icon-search{--icon-image: url(../res/search.svg)}.icon.icon-reset{--icon-image: url(../res/reset.svg)}nav{display:flex;overflow-x:auto}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}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(calc(12vw + 2px) + calc(2 * 0.7rem + 2 * 0.5rem + 1rem));bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}div.search-empty{margin:1.2rem;text-align:center}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:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:var(--highlight-color)}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.sticker>.icon{width:70%;height:70%;margin:15%}div.search-box{display:flex;margin:.5rem;padding:0;border-radius:.4rem;background-color:var(--search-box-color);opacity:.5;transition:all .1s;border:1px solid transparent}div.search-box:focus-within{opacity:1;border:1px solid var(--text-color)}div.search-box:not(:focus-within):hover{opacity:.7}div.search-box input,div.search-box .icon-display{color:var(--text-color);outline:none;border:none;height:1rem;margin:0;padding:.7rem;background-color:transparent;-webkit-tap-highlight-color:transparent}div.search-box .icon-display{width:1rem}div.search-box .icon-display.reset-click-zone{cursor:pointer}div.search-box .icon-display .icon{display:block;width:1rem;height:1rem}div.search-box .icon-display .icon-search{opacity:.5}div.search-box input{flex-grow:1;font-size:1rem}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input:not([type=checkbox]){width:100%}

View File

@ -92,9 +92,12 @@ main.theme-black
&.icon-recent &.icon-recent
--icon-image: url(../res/recent.svg) --icon-image: url(../res/recent.svg)
&.icon.icon-search &.icon-search
--icon-image: url(../res/search.svg) --icon-image: url(../res/search.svg)
&.icon-reset
--icon-image: url(../res/reset.svg)
nav nav
display: flex display: flex
overflow-x: auto overflow-x: auto
@ -166,30 +169,49 @@ div.sticker
margin: 15% margin: 15%
div.search-box div.search-box
position: relative
display: flex display: flex
margin: $search-box-input-margin
padding: 0
border-radius: .4rem
background-color: var(--search-box-color)
opacity: .5
transition: all .1s
border: 1px solid transparent
>input[type="text"] &:focus-within
flex-grow: 1 opacity: 1
background-color: var(--search-box-color) border: 1px solid var(--text-color)
&:not(:focus-within):hover
opacity: .7
input,.icon-display
color: var(--text-color)
outline: none outline: none
border: none border: none
border-radius: .25rem
height: $search-box-input-height height: $search-box-input-height
margin: 0
padding: $search-box-input-padding padding: $search-box-input-padding
padding-right: calc(#{$search-box-icon-size} + #{$search-box-input-padding}) background-color: transparent
margin: $search-box-input-margin -webkit-tap-highlight-color: transparent
font-size: 1rem
color: var(--text-color)
>span.icon .icon-display
display: flex width: $search-box-input-height
position: absolute
top: calc(50% - #{$search-box-icon-size} / 2) &.reset-click-zone
right: $search-box-icon-size cursor: pointer
width: $search-box-icon-size
height: $search-box-icon-size .icon
box-sizing: border-box display: block
width: $search-box-icon-size
height: $search-box-icon-size
.icon-search
opacity: .5
input
flex-grow: 1
font-size: 1rem
div.settings-list div.settings-list
display: flex display: flex
@ -202,5 +224,5 @@ div.settings-list
padding: .5rem padding: .5rem
border-radius: .25rem border-radius: .25rem
input input:not([type="checkbox"])
width: 100% width: 100%