@devvit/cli 0.11.18 → 0.11.19-next-2025-07-01-15-23-37-e346f8ae5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/util/package-managers/package-util.d.ts +3 -0
- package/dist/util/package-managers/package-util.d.ts.map +1 -1
- package/dist/util/project.d.ts.map +1 -1
- package/dist/util/project.js +21 -13
- package/dist/util/project.test.js +8 -7
- package/package.json +9 -10
- package/dist/templates/blocks-post/assets/logo.png +0 -0
- package/dist/templates/blocks-post/devvit.yaml +0 -1
- package/dist/templates/blocks-post/package.json +0 -13
- package/dist/templates/blocks-post/src/main.tsx +0 -58
- package/dist/templates/blocks-post/template-config.json +0 -4
- package/dist/templates/blocks-post/tsconfig.json +0 -3
- package/dist/templates/empty/devvit.yaml +0 -1
- package/dist/templates/empty/package.json +0 -13
- package/dist/templates/empty/src/main.tsx +0 -14
- package/dist/templates/empty/template-config.json +0 -4
- package/dist/templates/empty/tsconfig.json +0 -3
- package/dist/templates/payments/devvit.yaml +0 -1
- package/dist/templates/payments/package.json +0 -14
- package/dist/templates/payments/src/main.tsx +0 -134
- package/dist/templates/payments/src/products.json +0 -26
- package/dist/templates/payments/template-config.json +0 -4
- package/dist/templates/payments/tsconfig.json +0 -4
- package/dist/templates/payments-web-view/devvit.yaml +0 -1
- package/dist/templates/payments-web-view/package.json +0 -14
- package/dist/templates/payments-web-view/src/createPost.tsx +0 -23
- package/dist/templates/payments-web-view/src/main.tsx +0 -75
- package/dist/templates/payments-web-view/src/message.ts +0 -22
- package/dist/templates/payments-web-view/src/products.json +0 -13
- package/dist/templates/payments-web-view/template-config.json +0 -4
- package/dist/templates/payments-web-view/tsconfig.json +0 -7
- package/dist/templates/payments-web-view/webroot/page.html +0 -18
- package/dist/templates/payments-web-view/webroot/script.js +0 -62
- package/dist/templates/payments-web-view/webroot/style.css +0 -104
- package/dist/templates/pen/devvit.yaml +0 -1
- package/dist/templates/pen/package.json +0 -13
- package/dist/templates/pen/src/main.tsx +0 -46
- package/dist/templates/pen/template-config.json +0 -4
- package/dist/templates/pen/tsconfig.json +0 -3
- package/dist/templates/web-view-post/devvit.yaml +0 -1
- package/dist/templates/web-view-post/package.json +0 -13
- package/dist/templates/web-view-post/src/createPost.tsx +0 -23
- package/dist/templates/web-view-post/src/main.tsx +0 -99
- package/dist/templates/web-view-post/src/message.ts +0 -19
- package/dist/templates/web-view-post/template-config.json +0 -4
- package/dist/templates/web-view-post/tsconfig.json +0 -7
- package/dist/templates/web-view-post/webroot/page.html +0 -24
- package/dist/templates/web-view-post/webroot/script.js +0 -81
- package/dist/templates/web-view-post/webroot/style.css +0 -114
- package/dist/util/Bundler.test.d.ts +0 -2
- package/dist/util/Bundler.test.d.ts.map +0 -1
- package/dist/util/Bundler.test.js +0 -32
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/** Message from Devvit to the web view. */
|
|
2
|
-
export type DevvitMessage =
|
|
3
|
-
// Message from blocks to the webview. This tells the web view to update
|
|
4
|
-
// based on a purchase being made.
|
|
5
|
-
{ type: 'purchaseMade'; data: {} };
|
|
6
|
-
|
|
7
|
-
/** Message from the web view to Devvit. */
|
|
8
|
-
export type WebViewMessage =
|
|
9
|
-
// Message represents the user indicating they want to purchase something
|
|
10
|
-
// in the web-view. Note that this input should not be trusted, but needs
|
|
11
|
-
// to be validated in blocks when the message is received.
|
|
12
|
-
{ type: 'purchase'; data: { sku: string } };
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Web view MessageEvent listener data type. The Devvit API wraps all messages
|
|
16
|
-
* from Blocks to the web view.
|
|
17
|
-
*/
|
|
18
|
-
export type DevvitSystemMessage = {
|
|
19
|
-
data: { message: DevvitMessage };
|
|
20
|
-
/** Reserved type for messages sent via `context.ui.webView.postMessage`. */
|
|
21
|
-
type?: 'devvit-message' | string;
|
|
22
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://developers.reddit.com/schema/products.json",
|
|
3
|
-
"products": [
|
|
4
|
-
{
|
|
5
|
-
"sku": "webviewpurchase",
|
|
6
|
-
"displayName": "A Generous Purchase",
|
|
7
|
-
"description": "It's very kind you'd purchase this! Let's see what you get!",
|
|
8
|
-
"metadata": {},
|
|
9
|
-
"price": 25,
|
|
10
|
-
"accountingType": "CONSUMABLE"
|
|
11
|
-
}
|
|
12
|
-
]
|
|
13
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta
|
|
5
|
-
name="viewport"
|
|
6
|
-
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
|
|
7
|
-
/>
|
|
8
|
-
<meta charset="UTF-8" />
|
|
9
|
-
<title>Web View Payments Example</title>
|
|
10
|
-
<link rel="stylesheet" href="style.css" />
|
|
11
|
-
</head>
|
|
12
|
-
<div class="button-container">
|
|
13
|
-
<button id="btn-purchase">Purchase</button>
|
|
14
|
-
</div>
|
|
15
|
-
<text id="purchase-text"></text>
|
|
16
|
-
<script type="module" src="script.js"></script>
|
|
17
|
-
</body>
|
|
18
|
-
</html>
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/** @typedef {import('../src/message.ts').DevvitSystemMessage} DevvitSystemMessage */
|
|
2
|
-
/** @typedef {import('../src/message.ts').WebViewMessage} WebViewMessage */
|
|
3
|
-
|
|
4
|
-
class App {
|
|
5
|
-
constructor() {
|
|
6
|
-
// Get references to the HTML elements
|
|
7
|
-
this.purchaseButton = /** @type {HTMLButtonElement} */ (
|
|
8
|
-
document.querySelector('#btn-purchase')
|
|
9
|
-
);
|
|
10
|
-
this.purchaseText = document.querySelector('#purchase-text');
|
|
11
|
-
// When the Devvit app sends a message with `postMessage()`, this will be triggered
|
|
12
|
-
addEventListener('message', this.#onMessage);
|
|
13
|
-
|
|
14
|
-
this.purchaseButton.addEventListener('click', () => {
|
|
15
|
-
// Sends a message to the Devvit app
|
|
16
|
-
postWebViewMessage({ type: 'purchase', data: { sku: 'webviewpurchase' } });
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @arg {MessageEvent<DevvitSystemMessage>} ev
|
|
22
|
-
* @return {void}
|
|
23
|
-
*/
|
|
24
|
-
#onMessage = (ev) => {
|
|
25
|
-
// Reserved type for messages sent via `context.ui.webView.postMessage`
|
|
26
|
-
if (ev.data.type !== 'devvit-message') return;
|
|
27
|
-
const { message } = ev.data.data;
|
|
28
|
-
|
|
29
|
-
if (message.type === 'purchaseMade') {
|
|
30
|
-
const firstMessage = 'Thank you for your purchase 😊.';
|
|
31
|
-
const secondMessage = 'A second purchase! So generous!';
|
|
32
|
-
const thirdMessage = 'So kind to make another purchase.';
|
|
33
|
-
const lastMessage = 'Thank you so much!';
|
|
34
|
-
const currentText = this.purchaseText.innerHTML;
|
|
35
|
-
// Go first -> second -> third -> last -> third -> (loop the last two)
|
|
36
|
-
if (currentText === '') {
|
|
37
|
-
this.purchaseText.innerHTML = firstMessage;
|
|
38
|
-
} else if (currentText === firstMessage) {
|
|
39
|
-
this.purchaseText.innerHTML = secondMessage;
|
|
40
|
-
} else if (currentText === secondMessage) {
|
|
41
|
-
this.purchaseText.innerHTML = thirdMessage;
|
|
42
|
-
} else if (currentText === thirdMessage) {
|
|
43
|
-
this.purchaseText.innerHTML = lastMessage;
|
|
44
|
-
} else if (currentText === lastMessage) {
|
|
45
|
-
this.purchaseText.innerHTML = thirdMessage;
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
throw Error(`Unexpected message type: ${message.type}`);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Sends a message to the Devvit app.
|
|
55
|
-
* @arg {WebViewMessage} msg
|
|
56
|
-
* @return {void}
|
|
57
|
-
*/
|
|
58
|
-
function postWebViewMessage(msg) {
|
|
59
|
-
parent.postMessage(msg, '*');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
new App();
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
:root {
|
|
2
|
-
--primary-color: #6b7280;
|
|
3
|
-
--background-color: #f3f4f6;
|
|
4
|
-
--text-color: #374151;
|
|
5
|
-
--accent-color: #60a5fa;
|
|
6
|
-
--border-radius: 6px;
|
|
7
|
-
--shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
body {
|
|
11
|
-
display: flex;
|
|
12
|
-
flex-direction: column;
|
|
13
|
-
gap: 0.75rem;
|
|
14
|
-
padding: 1rem;
|
|
15
|
-
max-width: 650px;
|
|
16
|
-
margin: 0 auto;
|
|
17
|
-
min-height: 100vh;
|
|
18
|
-
background-color: var(--background-color);
|
|
19
|
-
color: var(--text-color);
|
|
20
|
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
21
|
-
line-height: 1.3;
|
|
22
|
-
position: fixed;
|
|
23
|
-
width: 100%;
|
|
24
|
-
height: 100%;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
h1,
|
|
28
|
-
h2,
|
|
29
|
-
h3 {
|
|
30
|
-
margin: 0;
|
|
31
|
-
color: var(--primary-color);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
h1 {
|
|
35
|
-
font-size: 1.8rem;
|
|
36
|
-
font-weight: 700;
|
|
37
|
-
margin-bottom: 0.5rem;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
h2 {
|
|
41
|
-
font-size: 1.3rem;
|
|
42
|
-
font-weight: 600;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
h3 {
|
|
46
|
-
font-size: 1.1rem;
|
|
47
|
-
font-weight: 500;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/* New button container */
|
|
51
|
-
.button-container {
|
|
52
|
-
display: flex;
|
|
53
|
-
gap: 0.5rem;
|
|
54
|
-
width: 100%;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
button {
|
|
58
|
-
flex: 1;
|
|
59
|
-
padding: 0.5rem 1rem;
|
|
60
|
-
border: none;
|
|
61
|
-
border-radius: var(--border-radius);
|
|
62
|
-
background-color: var(--accent-color);
|
|
63
|
-
color: white;
|
|
64
|
-
font-size: 0.9rem;
|
|
65
|
-
font-weight: 500;
|
|
66
|
-
cursor: pointer;
|
|
67
|
-
transition:
|
|
68
|
-
transform 0.2s,
|
|
69
|
-
box-shadow 0.2s;
|
|
70
|
-
box-shadow: var(--shadow);
|
|
71
|
-
margin: 0;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
button:hover {
|
|
75
|
-
transform: translateY(-1px);
|
|
76
|
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
button:active {
|
|
80
|
-
transform: translateY(0);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
pre {
|
|
84
|
-
background-color: white;
|
|
85
|
-
padding: 0.75rem;
|
|
86
|
-
border-radius: var(--border-radius);
|
|
87
|
-
box-shadow: var(--shadow);
|
|
88
|
-
overflow-x: auto;
|
|
89
|
-
font-family: 'Courier New', Courier, monospace;
|
|
90
|
-
border: 1px solid #e5e7eb;
|
|
91
|
-
font-size: 0.9rem;
|
|
92
|
-
margin: 0;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
@media (max-width: 640px) {
|
|
96
|
-
body {
|
|
97
|
-
padding: 0.75rem;
|
|
98
|
-
gap: 0.5rem;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
h1 {
|
|
102
|
-
font-size: 1.5rem;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
name: {{name}}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
{{&mainSrc}}
|
|
2
|
-
|
|
3
|
-
// Define what packages you want to use here
|
|
4
|
-
// Others include:
|
|
5
|
-
// kvStore: a simple key value store for persisting data across sessions within this installation
|
|
6
|
-
// media: used for importing and posting images
|
|
7
|
-
Devvit.configure({
|
|
8
|
-
redditAPI: true, // context.reddit will now be available
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
/*
|
|
12
|
-
* Use a menu action to create a new post
|
|
13
|
-
*/
|
|
14
|
-
Devvit.addMenuItem({
|
|
15
|
-
label: 'New experience',
|
|
16
|
-
location: 'subreddit',
|
|
17
|
-
/*
|
|
18
|
-
* _ tells Typescript we don't care about the first argument
|
|
19
|
-
* The second argument is a Context object--here we use object destructuring to
|
|
20
|
-
* pull just the parts we need. The code below is equivalient
|
|
21
|
-
* to using context.reddit and context.ui
|
|
22
|
-
*/
|
|
23
|
-
onPress: async (_, { reddit, ui }) => {
|
|
24
|
-
ui.showToast("Submitting your post - upon completion you'll navigate there.");
|
|
25
|
-
|
|
26
|
-
const subreddit = await reddit.getCurrentSubreddit();
|
|
27
|
-
|
|
28
|
-
/*
|
|
29
|
-
* Submits the post to the specified subreddit
|
|
30
|
-
*/
|
|
31
|
-
const post = await reddit.submitPost({
|
|
32
|
-
// This will show while your post is loading
|
|
33
|
-
preview: (
|
|
34
|
-
<vstack padding="medium" cornerRadius="medium">
|
|
35
|
-
<text style="heading" size="medium">
|
|
36
|
-
Loading hello world post...
|
|
37
|
-
</text>
|
|
38
|
-
</vstack>
|
|
39
|
-
),
|
|
40
|
-
title: `${subreddit.name} Hello World`,
|
|
41
|
-
subredditName: subreddit.name,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
ui.navigateTo(post);
|
|
45
|
-
},
|
|
46
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
name: {{name}}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Devvit } from '@devvit/public-api';
|
|
2
|
-
|
|
3
|
-
// Adds a new menu item to the subreddit allowing to create a new post
|
|
4
|
-
Devvit.addMenuItem({
|
|
5
|
-
label: 'Create New Devvit Post (with Web View)',
|
|
6
|
-
location: 'subreddit',
|
|
7
|
-
onPress: async (_event, context) => {
|
|
8
|
-
const { reddit, ui } = context;
|
|
9
|
-
const subreddit = await reddit.getCurrentSubreddit();
|
|
10
|
-
const post = await reddit.submitPost({
|
|
11
|
-
title: 'Web View Example',
|
|
12
|
-
subredditName: subreddit.name,
|
|
13
|
-
// The preview appears while the post loads
|
|
14
|
-
preview: (
|
|
15
|
-
<vstack height="100%" width="100%" alignment="middle center">
|
|
16
|
-
<text size="large">Loading ...</text>
|
|
17
|
-
</vstack>
|
|
18
|
-
),
|
|
19
|
-
});
|
|
20
|
-
ui.showToast({ text: 'Created post!' });
|
|
21
|
-
ui.navigateTo(post);
|
|
22
|
-
},
|
|
23
|
-
});
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import './createPost.js';
|
|
2
|
-
|
|
3
|
-
import { Devvit, useState, useWebView } from '@devvit/public-api';
|
|
4
|
-
|
|
5
|
-
import type { DevvitMessage, WebViewMessage } from './message.js';
|
|
6
|
-
|
|
7
|
-
Devvit.configure({
|
|
8
|
-
redditAPI: true,
|
|
9
|
-
redis: true,
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
// Add a custom post type to Devvit
|
|
13
|
-
Devvit.addCustomPostType({
|
|
14
|
-
name: 'Web View Example',
|
|
15
|
-
height: 'tall',
|
|
16
|
-
render: (context) => {
|
|
17
|
-
// Load username with `useAsync` hook
|
|
18
|
-
const [username] = useState(async () => {
|
|
19
|
-
return (await context.reddit.getCurrentUsername()) ?? 'anon';
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// Load latest counter from redis with `useAsync` hook
|
|
23
|
-
const [counter, setCounter] = useState(async () => {
|
|
24
|
-
const redisCount = await context.redis.get(`counter_${context.postId}`);
|
|
25
|
-
return Number(redisCount ?? 0);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
const webView = useWebView<WebViewMessage, DevvitMessage>({
|
|
29
|
-
// URL of your web view content
|
|
30
|
-
url: 'page.html',
|
|
31
|
-
|
|
32
|
-
// Handle messages sent from the web view
|
|
33
|
-
async onMessage(message, webView) {
|
|
34
|
-
switch (message.type) {
|
|
35
|
-
case 'webViewReady':
|
|
36
|
-
webView.postMessage({
|
|
37
|
-
type: 'initialData',
|
|
38
|
-
data: {
|
|
39
|
-
username: username,
|
|
40
|
-
currentCounter: counter,
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
break;
|
|
44
|
-
case 'setCounter':
|
|
45
|
-
await context.redis.set(
|
|
46
|
-
`counter_${context.postId}`,
|
|
47
|
-
message.data.newCounter.toString()
|
|
48
|
-
);
|
|
49
|
-
setCounter(message.data.newCounter);
|
|
50
|
-
|
|
51
|
-
webView.postMessage({
|
|
52
|
-
type: 'updateCounter',
|
|
53
|
-
data: {
|
|
54
|
-
currentCounter: message.data.newCounter,
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
break;
|
|
58
|
-
default:
|
|
59
|
-
throw new Error(`Unknown message type: ${message satisfies never}`);
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
onUnmount() {
|
|
63
|
-
context.ui.showToast('Web view closed!');
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// Render the custom post type
|
|
68
|
-
return (
|
|
69
|
-
<vstack grow padding="small">
|
|
70
|
-
<vstack grow alignment="middle center">
|
|
71
|
-
<text size="xlarge" weight="bold">
|
|
72
|
-
Example App
|
|
73
|
-
</text>
|
|
74
|
-
<spacer />
|
|
75
|
-
<vstack alignment="start middle">
|
|
76
|
-
<hstack>
|
|
77
|
-
<text size="medium">Username:</text>
|
|
78
|
-
<text size="medium" weight="bold">
|
|
79
|
-
{' '}
|
|
80
|
-
{username ?? ''}
|
|
81
|
-
</text>
|
|
82
|
-
</hstack>
|
|
83
|
-
<hstack>
|
|
84
|
-
<text size="medium">Current counter:</text>
|
|
85
|
-
<text size="medium" weight="bold">
|
|
86
|
-
{' '}
|
|
87
|
-
{counter ?? ''}
|
|
88
|
-
</text>
|
|
89
|
-
</hstack>
|
|
90
|
-
</vstack>
|
|
91
|
-
<spacer />
|
|
92
|
-
<button onPress={() => webView.mount()}>Launch App</button>
|
|
93
|
-
</vstack>
|
|
94
|
-
</vstack>
|
|
95
|
-
);
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
export default Devvit;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/** Message from Devvit to the web view. */
|
|
2
|
-
export type DevvitMessage =
|
|
3
|
-
| { type: 'initialData'; data: { username: string; currentCounter: number } }
|
|
4
|
-
| { type: 'updateCounter'; data: { currentCounter: number } };
|
|
5
|
-
|
|
6
|
-
/** Message from the web view to Devvit. */
|
|
7
|
-
export type WebViewMessage =
|
|
8
|
-
| { type: 'webViewReady' }
|
|
9
|
-
| { type: 'setCounter'; data: { newCounter: number } };
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Web view MessageEvent listener data type. The Devvit API wraps all messages
|
|
13
|
-
* from Blocks to the web view.
|
|
14
|
-
*/
|
|
15
|
-
export type DevvitSystemMessage = {
|
|
16
|
-
data: { message: DevvitMessage };
|
|
17
|
-
/** Reserved type for messages sent via `context.ui.webView.postMessage`. */
|
|
18
|
-
type?: 'devvit-message' | string;
|
|
19
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta
|
|
5
|
-
name="viewport"
|
|
6
|
-
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
|
|
7
|
-
/>
|
|
8
|
-
<meta charset="UTF-8" />
|
|
9
|
-
<title>Web View Example</title>
|
|
10
|
-
<link rel="stylesheet" href="style.css" />
|
|
11
|
-
</head>
|
|
12
|
-
<body>
|
|
13
|
-
<h1>This is your page.html</h1>
|
|
14
|
-
<h3>Your username is <span id="username">Guest</span></h3>
|
|
15
|
-
<h3>Current counter is at: <span id="counter">0</span></h3>
|
|
16
|
-
<div class="button-container">
|
|
17
|
-
<button id="btn-increase">Increase Counter ⬆️</button>
|
|
18
|
-
<button id="btn-decrease">Decrease Counter ⬇️</button>
|
|
19
|
-
</div>
|
|
20
|
-
<h3>Last message from Devvit blocks:</h3>
|
|
21
|
-
<pre id="messageOutput"></pre>
|
|
22
|
-
<script type="application/javascript" src="script.js"></script>
|
|
23
|
-
</body>
|
|
24
|
-
</html>
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/** @typedef {import('../src/message.ts').DevvitSystemMessage} DevvitSystemMessage */
|
|
2
|
-
/** @typedef {import('../src/message.ts').WebViewMessage} WebViewMessage */
|
|
3
|
-
|
|
4
|
-
class App {
|
|
5
|
-
constructor() {
|
|
6
|
-
// Get references to the HTML elements
|
|
7
|
-
this.output = /** @type {HTMLPreElement} */ (document.querySelector('#messageOutput'));
|
|
8
|
-
this.increaseButton = /** @type {HTMLButtonElement} */ (
|
|
9
|
-
document.querySelector('#btn-increase')
|
|
10
|
-
);
|
|
11
|
-
this.decreaseButton = /** @type {HTMLButtonElement} */ (
|
|
12
|
-
document.querySelector('#btn-decrease')
|
|
13
|
-
);
|
|
14
|
-
this.usernameLabel = /** @type {HTMLSpanElement} */ (document.querySelector('#username'));
|
|
15
|
-
this.counterLabel = /** @type {HTMLSpanElement} */ (document.querySelector('#counter'));
|
|
16
|
-
this.counter = 0;
|
|
17
|
-
|
|
18
|
-
// When the Devvit app sends a message with `postMessage()`, this will be triggered
|
|
19
|
-
addEventListener('message', this.#onMessage);
|
|
20
|
-
|
|
21
|
-
// This event gets called when the web view is loaded
|
|
22
|
-
addEventListener('load', () => {
|
|
23
|
-
postWebViewMessage({ type: 'webViewReady' });
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
this.increaseButton.addEventListener('click', () => {
|
|
27
|
-
// Sends a message to the Devvit app
|
|
28
|
-
postWebViewMessage({ type: 'setCounter', data: { newCounter: this.counter + 1 } });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
this.decreaseButton.addEventListener('click', () => {
|
|
32
|
-
// Sends a message to the Devvit app
|
|
33
|
-
postWebViewMessage({ type: 'setCounter', data: { newCounter: this.counter - 1 } });
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @arg {MessageEvent<DevvitSystemMessage>} ev
|
|
39
|
-
* @return {void}
|
|
40
|
-
*/
|
|
41
|
-
#onMessage = (ev) => {
|
|
42
|
-
// Reserved type for messages sent via `context.ui.webView.postMessage`
|
|
43
|
-
if (ev.data.type !== 'devvit-message') return;
|
|
44
|
-
const { message } = ev.data.data;
|
|
45
|
-
|
|
46
|
-
// Always output full message
|
|
47
|
-
this.output.replaceChildren(JSON.stringify(message, undefined, 2));
|
|
48
|
-
|
|
49
|
-
switch (message.type) {
|
|
50
|
-
case 'initialData': {
|
|
51
|
-
// Load initial data
|
|
52
|
-
const { username, currentCounter } = message.data;
|
|
53
|
-
this.usernameLabel.innerText = username;
|
|
54
|
-
this.counter = currentCounter;
|
|
55
|
-
this.counterLabel.innerText = `${this.counter}`;
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
case 'updateCounter': {
|
|
59
|
-
const { currentCounter } = message.data;
|
|
60
|
-
this.counter = currentCounter;
|
|
61
|
-
this.counterLabel.innerText = `${this.counter}`;
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
default:
|
|
65
|
-
/** to-do: @satisifes {never} */
|
|
66
|
-
const _ = message;
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Sends a message to the Devvit app.
|
|
74
|
-
* @arg {WebViewMessage} msg
|
|
75
|
-
* @return {void}
|
|
76
|
-
*/
|
|
77
|
-
function postWebViewMessage(msg) {
|
|
78
|
-
parent.postMessage(msg, '*');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
new App();
|