diff --git a/marko/page/logs-content/components/page/component.js b/marko/page/logs-content/components/page/component.js index 88ff022..28eba57 100644 --- a/marko/page/logs-content/components/page/component.js +++ b/marko/page/logs-content/components/page/component.js @@ -1,5 +1,7 @@ const moment = require("moment") +const {postApi} = require("../../../../../rpdata/api/Post") + module.exports = class { onCreate(input) { this.state = { @@ -7,6 +9,46 @@ module.exports = class { } } + /** + * Patch the posts + * + * @param {{id:string, [x:string]: any}[]} patches + */ + patch(patches) { + const posts = this.state.log.posts.slice() + + for (const patch of patches) { + const index = posts.findIndex(p => p.id === patch.id) + if (index == -1) { + console.warn("Post not found for patch:", patch) + // TODO: Force full refresh + continue + } + + posts[index] = Object.assign(Object.assign({}, posts[index]), patch) + } + + // Sort posts and look for continuity issues (duplicate or skipped positions) + posts.sort((a, b) => a.position - b.position) + for (let i = 1; i < posts.length; ++i) { + if (posts[i].position !== posts[i - 1].position + 1) { + console.warn("Discontinuity detected!") + // TODO: Force full refresh + } + } + + this.state.log.posts = posts + this.state.log = Object.assign({}, this.state.log) + } + + movePost(post, toPosition) { + postApi.move({id: post.id, toPosition}).then(patches => { + this.patch(patches) + }).catch(err => { + console.warn(err) + }) + } + get title() { if (this.state.log.title) { return this.state.log.title diff --git a/marko/page/logs-content/components/page/index.marko b/marko/page/logs-content/components/page/index.marko index 21b990b..47603f3 100644 --- a/marko/page/logs-content/components/page/index.marko +++ b/marko/page/logs-content/components/page/index.marko @@ -2,6 +2,6 @@

${component.title}

- +
\ No newline at end of file diff --git a/marko/page/logs-content/components/post/component.js b/marko/page/logs-content/components/post/component.js index d761573..14f6e5f 100644 --- a/marko/page/logs-content/components/post/component.js +++ b/marko/page/logs-content/components/post/component.js @@ -4,11 +4,24 @@ module.exports = class { shortName: "", name: "", text: "", + modal: null, } this.updatePost(input) } + open(modal) { + this.state.modal == modal + } + + close() { + this.state.modal = null + } + + move(toPosition) { + this.emit("move", toPosition) + } + onInput(input) { if (this.state == null) { return diff --git a/marko/page/logs-content/components/post/index.marko b/marko/page/logs-content/components/post/index.marko index 93b20fd..6a9da55 100644 --- a/marko/page/logs-content/components/post/index.marko +++ b/marko/page/logs-content/components/post/index.marko @@ -1,5 +1,11 @@
+
+ Up + Down + + +
diff --git a/marko/page/logs-content/components/post/style.less b/marko/page/logs-content/components/post/style.less index c65c594..da74ae1 100644 --- a/marko/page/logs-content/components/post/style.less +++ b/marko/page/logs-content/components/post/style.less @@ -1,11 +1,31 @@ div.post { margin-bottom: 1ch; - .post-meta-row { + div.post-meta-row { padding: 0; margin: 0; } - + + div.options { + position: absolute; + text-align: right; + width: calc(100% - 35.5ch); + max-width: 75ch; + + a { + display: inline-block; + padding: 0 0.5ch; + + opacity: 0.5; + + cursor: pointer; + user-select: none; + } + a:hover { + opacity: 1; + } + } + div.post-body { font-size: 1.25em; diff --git a/rpdata/api/Post.js b/rpdata/api/Post.js index d57cefb..a71c90f 100644 --- a/rpdata/api/Post.js +++ b/rpdata/api/Post.js @@ -23,4 +23,25 @@ class Post { } } -module.exports = {Post} \ No newline at end of file +class PostAPI { + /** + * Call `movePost(input)` mutation, returns the id and position of all affected posts. + * + * @param {{id:string, toPosition: number}} input + * @returns {Promise<{id:string, position:number}[]>} + */ + move(input) { + return query(` + mutation MovePost($input: PostMoveInput!) { + movePost(input: $input) { + id + position + } + } + `, {input}, {permissions: ["post.move"]}).then(({movePost}) => { + return movePost + }) + } +} + +module.exports = {Post, postApi: new PostAPI} \ No newline at end of file