Tulir Asokan
4 years ago
3 changed files with 80 additions and 68 deletions
-
70web/index.js
-
31web/spinner.js
-
47web/widget-api.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 "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`
|
|||
<div style="width: ${size}; height: ${size}; margin: ${noInnerMargin ? 0 : margin} 0;" |
|||
class="sk-chase ${green && "green"}"> |
|||
<div class="sk-chase-dot" /> |
|||
<div class="sk-chase-dot" /> |
|||
<div class="sk-chase-dot" /> |
|||
<div class="sk-chase-dot" /> |
|||
<div class="sk-chase-dot" /> |
|||
<div class="sk-chase-dot" /> |
|||
</div> |
|||
`
|
|||
if (!noCenter) { |
|||
return html`<div style="margin: ${margin} 0;" class="sk-center-wrapper">${comp}</div>` |
|||
} |
|||
return comp |
|||
} |
@ -0,0 +1,47 @@ |
|||
// 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 |
|||
} |
|||
|
|||
window.parent.postMessage({ |
|||
...request, |
|||
response: request.action === "capabilities" ? { |
|||
capabilities: ["m.sticker"], |
|||
} : { |
|||
error: { message: "Action not supported" }, |
|||
}, |
|||
}, event.origin) |
|||
} |
|||
|
|||
export function sendSticker(content) { |
|||
window.parent.postMessage({ |
|||
api: "fromWidget", |
|||
action: "m.sticker", |
|||
requestId: `sticker-${Date.now()}`, |
|||
widgetId, |
|||
data: { |
|||
name: content.body, |
|||
content, |
|||
}, |
|||
}, "*") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue