Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## 0.1.1

- Add `THIRD-PARTY-NOTICES` with the copyright and MIT license texts of
pusher-js, @stablelib/base64, @stablelib/utf8 and qrcode-generator, which
`dist/cp.js` bundles, and a short notice at the top of `dist/cp.js`.
- Events are sent after the response, so a save never waits on Hibira.
- Comment events carry only the comment's ID and pages fetch the comment,
so long comments in any script appear live on every plan.
- The comment file is encoded before it is rewritten, and comments with
invalid UTF-8 are refused, so a failed write can't empty it.
- The comments card shows when loading failed, with a retry button, and
clears an old error after a later success.
- README: Statamic Pro, what passes through Hibira (hosted in Hong Kong),
plan limits and support contact.

## 0.1.0

- Avatars of everyone who has the same entry open, and a warning when one
Expand Down
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Realtime teamwork for the [Statamic](https://statamic.com) control panel, on
Works with Statamic 5 and 6, flat file or database. Comments need no
database, and there is nothing to publish or build.

Avatars, saved warnings and shared comments are about several people
working in the same control panel, so they need more than one control panel
user, which in Statamic generally means [Statamic Pro](https://statamic.com/pricing).
Live preview on another device is useful on Statamic Solo too.

![The dock, comments and a saved warning](art/comments-light.png)

## Install
Expand Down Expand Up @@ -88,15 +93,47 @@ don't use such an addon.
- Each entry has a presence channel. The control panel signs subscriptions
on its own route, for users who may view the entry, so you don't have to
change your app's broadcasting setup.
- Comments are stored first, then sent to the others on the entry's channel.
A page that lost its connection fetches them again when it reconnects.
- Comments are stored first. The others on the entry's channel get only the
comment's ID and fetch the comment from the control panel, so long
comments in any script go live. A page that lost its connection fetches
them again when it reconnects.
- Events are sent after the response, so saving never waits on Hibira, even
if it can't be reached.
- A live preview page opened outside the control panel listens on a public
channel named after a keyed hash of its preview token, so only someone with
the link can find it, and its events carry no content. Events are sent only
while such a page is open.
- Each open edit page is one connection, and each preview page opened on
another device is one more.

## Your data and Hibira

Events go through [Hibira](https://hibira.io), a hosted WebSocket service
whose servers are in Hong Kong. What passes through it:

- the name (or email, when no name is set), initials and avatar URL of each
control panel user who has an entry open, and the name of whoever saved
it;
- entry and comment IDs, so open pages know to fetch comments again. The
comment text itself stays on your server and is fetched from your own
control panel;
- for live preview on another device, a reload signal with no content.

See the [privacy policy](https://hibira.io/legal/privacy).

## Plan limits

Each open edit page is one connection, and each preview page opened on
another device is one more. The free plan allows 100 concurrent connections
and one app, which fits a small team; paid plans raise the connection, daily
message and message size limits. Comments are never limited by message size,
because only their IDs are sent. See [hibira.io/pricing](https://hibira.io/pricing).

## Support

Open an issue at [github.com/hibira-io/statamic/issues](https://github.com/hibira-io/statamic/issues)
or write to [support@hibira.io](mailto:support@hibira.io).

## Testing

```sh
Expand All @@ -108,12 +145,16 @@ with `npm ci && npm run build`.

## License

MIT
MIT. `dist/cp.js` includes pusher-js, @stablelib/base64, @stablelib/utf8 and
qrcode-generator, all MIT licensed; their notices are in
[THIRD-PARTY-NOTICES](THIRD-PARTY-NOTICES).

## 中文摘要

在 Statamic 控制台加上即時協作:同一篇內容誰也開著(頭像),有人儲存時立刻提醒重新載入;
即時留言(可標記已解決,只能看不能改的審稿人也能留言);用手機掃 QR Code 開即時預覽,
每次修改內容手機頁面就自動重新載入。支援 Statamic 5 與 6,留言存成 JSON 檔不需要資料庫。
多人協作功能需要多個控制台使用者(通常是 Statamic Pro)。使用者名稱、頭像網址與留言 ID 會經過位於香港的 Hibira;
留言內容留在你的伺服器上。免費方案 100 個同時連線、1 個 App。客服:support@hibira.io。
安裝:`composer require hibira-io/statamic`,`php artisan hibira:install` 填入
[hibira.io/portal](https://hibira.io/portal) 的 App ID、Key、Secret,並把網站網址加到 Allowed origins。
81 changes: 81 additions & 0 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
THIRD-PARTY NOTICES

dist/cp.js, the control panel script of this addon, includes code from the
open source projects below. Each is distributed under the MIT License, whose
text follows each notice.

-------------------------------------------------------------------------------
pusher-js 8.6.0
https://github.com/pusher/pusher-js

Copyright (c) 2020 Pusher, Ltd (https://pusher.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-------------------------------------------------------------------------------
@stablelib/base64 and @stablelib/utf8 (bundled inside pusher-js)
https://github.com/StableLib/stablelib

Copyright (C) 2016 Dmitry Chestnykh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-------------------------------------------------------------------------------
qrcode-generator 1.5.2
https://github.com/kazuhikoarase/qrcode-generator

Copyright (c) 2009 Kazuhiko Arase

URL: http://www.d-project.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

The word "QR Code" is a registered trademark of DENSO WAVE INCORPORATED.
23 changes: 18 additions & 5 deletions dist/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"build": "esbuild resources/js/cp.js --bundle --minify --format=iife --target=es2019 --loader:.css=text --outfile=dist/cp.js --legal-comments=none && esbuild resources/js/preview.js --minify --target=es2017 --outfile=dist/preview.js --legal-comments=none"
"build": "esbuild resources/js/cp.js --bundle --minify --format=iife --target=es2019 --loader:.css=text --outfile=dist/cp.js --legal-comments=eof \"--banner:js=/*! Hibira Realtime for Statamic. MIT License. Includes pusher-js (MIT, Copyright (c) 2020 Pusher, Ltd), @stablelib/base64 and @stablelib/utf8 (MIT, Copyright (C) 2016 Dmitry Chestnykh) and qrcode-generator (MIT, Copyright (c) 2009 Kazuhiko Arase). Full license texts: THIRD-PARTY-NOTICES. */\" && esbuild resources/js/preview.js --minify --target=es2017 --outfile=dist/preview.js --legal-comments=none"
},
"devDependencies": {
"esbuild": "^0.25.0",
Expand Down
1 change: 1 addition & 0 deletions resources/js/cp.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
.hibira-st-text-button:hover { background: var(--h-soft); color: var(--h-fg); }
.hibira-st-list { max-height: min(55vh, 460px); overflow-y: auto; padding: 4px 0; }
.hibira-st-empty { padding: 20px 16px; color: var(--h-muted); text-align: center; }
.hibira-st-empty .hibira-st-text-button { margin-top: 8px; }
.hibira-st-comment { display: flex; gap: 10px; padding: 10px 14px; }
.hibira-st-comment + .hibira-st-comment { border-top: 1px solid var(--h-soft); }
.hibira-st-comment.hibira-st-resolved { opacity: .65; }
Expand Down
84 changes: 71 additions & 13 deletions resources/js/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ class EntryPage {
this.channelName = 'presence-hibira-statamic.entry.' + entryId;
this.comments = [];
this.loaded = false;
this.loadFailed = false;
this.loading = null;
this.reloadAfter = false;
this.showResolved = false;
this.open = null;
this.unread = false;
Expand Down Expand Up @@ -273,7 +276,7 @@ class EntryPage {
this.channel.bind('pusher:member_added', () => this.renderPeople());
this.channel.bind('pusher:member_removed', () => this.renderPeople());
this.channel.bind('saved', (data) => this.onSaved(data));
this.channel.bind('comment', (data) => this.onComment(data));
this.channel.bind('comment', (data) => this.onRemoteComment(data));
}

others() {
Expand Down Expand Up @@ -326,22 +329,54 @@ class EntryPage {

// -- Comments --------------------------------------------------------------

async loadComments() {
try {
const data = await api(route('comments', this.entryId));
this.comments = data.comments || [];
this.loaded = true;
// Fetches the comments. Calls made while a fetch runs are folded into
// one more fetch after it, so a burst of events costs two requests.
loadComments() {
if (this.loading) {
this.reloadAfter = true;
return this.loading;
}

this.loading = (async () => {
try {
const data = await api(route('comments', this.entryId));
const known = new Set(this.comments.map((c) => c.id));
this.comments = data.comments || [];
if (this.loaded && this.open !== 'comments' && this.comments.some((c) => !known.has(c.id) && String(c.author.id) !== String(config.user.id))) {
this.unread = true;
}
this.loaded = true;
this.loadFailed = false;
} catch (e) {
// Shown in the card with a retry button; a reconnect also tries again.
this.loadFailed = true;
} finally {
this.loading = null;
}
this.renderCount();
if (this.open === 'comments') this.renderComments();
} catch (e) {
// Shown as an empty list; the next reconnect tries again.
if (this.reloadAfter) {
this.reloadAfter = false;
this.loadComments();
}
})();

return this.loading;
}

// Events from the others carry only the comment ID, so a long comment
// never meets the message size limit: fetch the comments again.
onRemoteComment({ action, id }) {
if (action === 'deleted') {
this.onComment({ action, comment: { id } });
} else {
this.loadComments();
}
}

onComment({ action, comment }) {
if (action === 'added') {
if (!this.comments.some((c) => c.id === comment.id)) this.comments.push(comment);
if (this.open !== 'comments' && String(comment.author.id) !== String(config.user.id)) this.unread = true;
} else if (action === 'updated') {
this.comments = this.comments.map((c) => (c.id === comment.id ? comment : c));
} else if (action === 'deleted') {
Expand Down Expand Up @@ -378,13 +413,30 @@ class EntryPage {
const atEnd = list.scrollTop + list.clientHeight >= list.scrollHeight - 8;
const scrollTop = list.scrollTop;

list.replaceChildren(...(visible.length
? visible.map((comment) => this.renderComment(comment))
: [h('div', { class: 'hibira-st-empty' }, this.loaded ? t('empty') : '…')]));
let placeholder;
if (this.loadFailed && !this.loaded) {
placeholder = h('div', { class: 'hibira-st-empty' },
h('div', {}, t('load_failed')),
h('button', { type: 'button', class: 'hibira-st-text-button', onclick: () => this.retryLoad() }, t('retry')));
} else {
placeholder = h('div', { class: 'hibira-st-empty', 'aria-busy': this.loaded ? 'false' : 'true' }, this.loaded ? t('empty') : '…');
}

list.replaceChildren(...(visible.length ? visible.map((comment) => this.renderComment(comment)) : [placeholder]));

list.scrollTop = atEnd ? list.scrollHeight : scrollTop;
}

retryLoad() {
this.loadFailed = false;
this.renderComments();
this.loadComments();
}

clearError() {
if (this.error) this.error.textContent = '';
}

buildCommentsCard() {
this.resolvedToggle = h('button', {
type: 'button',
Expand All @@ -400,7 +452,10 @@ class EntryPage {
placeholder: t('placeholder'),
'aria-label': t('placeholder'),
maxlength: config.maxLength,
oninput: () => this.resize(),
oninput: () => {
this.clearError();
this.resize();
},
onkeydown: (event) => {
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
Expand Down Expand Up @@ -481,6 +536,7 @@ class EntryPage {
try {
const { comment } = await api(route('comments', this.entryId), { method: 'POST', body: { body } });
this.textarea.value = '';
this.clearError();
this.onComment({ action: 'added', comment });
if (this.list) this.list.scrollTop = this.list.scrollHeight;
} catch (e) {
Expand All @@ -494,6 +550,7 @@ class EntryPage {
async resolve(comment, resolved) {
try {
const data = await api(route('resolve', this.entryId, comment.id), { method: 'POST', body: { resolved } });
this.clearError();
this.onComment({ action: 'updated', comment: data.comment });
} catch (e) {
this.error.textContent = t('failed');
Expand All @@ -504,6 +561,7 @@ class EntryPage {
this.confirming = null;
try {
await api(route('comment', this.entryId, comment.id), { method: 'DELETE' });
this.clearError();
this.onComment({ action: 'deleted', comment: { id: comment.id } });
} catch (e) {
this.renderComments();
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'resolved_by' => 'Resolved by :name',
'close' => 'Close',
'failed' => 'Couldn’t save. Try again.',
'load_failed' => 'Couldn’t load the comments.',
'retry' => 'Try again',
'too_long' => 'Comments can be up to :max characters.',
'new_comment' => ':name commented',
'you' => 'You',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/ja/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'resolved_by' => ':name さんが解決済みにしました',
'close' => '閉じる',
'failed' => '保存できませんでした。もう一度お試しください。',
'load_failed' => 'コメントを読み込めませんでした。',
'retry' => '再試行',
'too_long' => 'コメントは :max 文字までです。',
'new_comment' => ':name さんがコメントしました',
'you' => 'あなた',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/ko/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'resolved_by' => ':name 님이 해결함',
'close' => '닫기',
'failed' => '저장하지 못했습니다. 다시 시도하세요.',
'load_failed' => '댓글을 불러오지 못했습니다.',
'retry' => '다시 시도',
'too_long' => '댓글은 최대 :max자까지 쓸 수 있습니다.',
'new_comment' => ':name 님이 댓글을 남겼습니다',
'you' => '나',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/th/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'resolved_by' => ':name ทำเครื่องหมายว่าแก้ไขแล้ว',
'close' => 'ปิด',
'failed' => 'บันทึกไม่สำเร็จ โปรดลองอีกครั้ง',
'load_failed' => 'โหลดความคิดเห็นไม่สำเร็จ',
'retry' => 'ลองอีกครั้ง',
'too_long' => 'ความคิดเห็นยาวได้ไม่เกิน :max ตัวอักษร',
'new_comment' => ':name แสดงความคิดเห็น',
'you' => 'คุณ',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/vi/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'resolved_by' => ':name đã đánh dấu xử lý',
'close' => 'Đóng',
'failed' => 'Không lưu được. Hãy thử lại.',
'load_failed' => 'Không tải được bình luận.',
'retry' => 'Thử lại',
'too_long' => 'Bình luận tối đa :max ký tự.',
'new_comment' => ':name đã bình luận',
'you' => 'Bạn',
Expand Down
Loading
Loading