Skip to content

Commit

Permalink
Allow editing scope as well
Browse files Browse the repository at this point in the history
  • Loading branch information
lynn committed Apr 3, 2024
1 parent 95e73c6 commit 6aae8f6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
13 changes: 11 additions & 2 deletions core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,24 @@ actions.edit = guard(
{
id: checks.goodid,
body: checks.nobomb,
scope: checks.scope,
},
(ret, i, uname) => {
let word = by_id(i.id);
if (word.user !== uname) {
return ret(flip('you are not the owner of this entry'));
}
word.body = replacements(i.body);
const new_body = replacements(i.body);
const body_changed = word.body !== new_body;
const scope_changed = word.scope !== i.scope;
word.body = new_body;
word.scope = i.scope;
ret(good({ entry: present(word, uname) }));
emitter.emit('edit', word);
if (body_changed) {
emitter.emit('edit', word);
} else if (scope_changed) {
emitter.emit('move', word);
}
},
);

Expand Down
8 changes: 4 additions & 4 deletions frontend/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
:theme="theme"
@note="s => note(result, s)"
@removenote="date => removenote(result, date)"
@edit="body => edit(result, body)"
@edit="(body, scope) => edit(result, body, scope)"
@uncollapse="
results.forEach(r => (r.uncollapsed = false));
result.uncollapsed = true;
Expand Down Expand Up @@ -385,10 +385,10 @@ export default defineComponent({
});
},
edit(whom: Entry, body: string): void {
edit(whom: Entry, body: string, scope: string): void {
// Update the entry early to prevent a flash of the old body...
this.update_entry(whom, { body });
this.apisend({ action: 'edit', id: whom.id, body }, data => {
this.update_entry(whom, { body, scope });
this.apisend({ action: 'edit', id: whom.id, body, scope }, data => {
// ...but let the API response have the final word:
this.update_entry(whom, data.entry);
});
Expand Down
38 changes: 28 additions & 10 deletions frontend/Result.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ defineProps<{
>
</h2>
<span class="info">
<input
v-if="editing"
type="text"
size="5"
v-model="new_scope"
class="scope editing"
autocomplete="language"
list="common-languages"
@keypress.enter.exact.prevent="submit_edit"
/>
<a
v-if="!editing"
:href="'#scope:' + result.scope"
class="scope"
@click="navigate('scope:' + result.scope)"
Expand All @@ -53,7 +64,7 @@ defineProps<{
rows="1"
placeholder="Enter a definition using slots (example: _&hairsp;_&hairsp;_ likes _&hairsp;_&hairsp;_)"
@input="set_new_body"
:value.sync="edit_body"
:value.sync="new_body"
@keypress.enter.exact.prevent="submit_edit"
autocomplete="off"
autocorrect="on"
Expand Down Expand Up @@ -109,7 +120,15 @@ defineProps<{
</p>
</form>
</div>
<ul class="controls" v-if="username">
<ul class="controls" v-if="username && editing">
<li>
<input type="button" value="submit" @click="submit_edit" />
</li>
<li>
<input type="button" value="cancel" @click="editing = false" />
</li>
</ul>
<ul class="controls" v-if="username && !editing">
<li v-if="!result.uncollapsed">
<input type="button" value="add note" @click="$emit('uncollapse')" />
<!-- TODO: for some reason this doesn't work on second, third… try. jfc -->
Expand Down Expand Up @@ -156,12 +175,9 @@ defineProps<{
<li>
<input type="button" value="fork" @click="$emit('fork')" />
</li>
<li v-if="username == result.user && !editing">
<li v-if="username == result.user">
<input type="button" value="edit" @click="start_edit" />
</li>
<li v-if="username == result.user && editing">
<input type="button" value="submit" @click="submit_edit" />
</li>
</ul>
</div>
</template>
Expand Down Expand Up @@ -217,20 +233,21 @@ export default defineComponent({
start_edit(): void {
this.editing = true;
this.edit_body = this.result.body;
this.new_body = this.result.body;
this.new_scope = this.result.scope;
},
set_new_body(event: Event): void {
const target = event.target as HTMLTextAreaElement;
target.value = this.edit_body = shared.replacements(
target.value = this.new_body = shared.replacements(
target.value,
true,
true,
);
},
submit_edit(): void {
this.$emit('edit', this.edit_body);
this.$emit('edit', this.new_body, this.new_scope);
this.editing = false;
},
},
Expand All @@ -239,7 +256,8 @@ export default defineComponent({
hesitating: false,
input: '',
editing: false,
edit_body: '',
new_body: '',
new_scope: '',
};
},
computed: {
Expand Down
5 changes: 3 additions & 2 deletions frontend/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ footer .controls {
margin-left: 0.25em;
}

.card .scope {
.card .scope:not(.editing) {
color: inherit;
opacity: 0.5;
}
Expand Down Expand Up @@ -284,7 +284,8 @@ textarea {
padding-bottom: 16px;
}

textarea.editing {
.editing {
color: inherit;
background-color: #0000000d;
}

Expand Down
19 changes: 16 additions & 3 deletions modules/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import request from 'request-promise-native';
import { Entry, Note } from '../core/commons.js';
import * as shared from '../frontend/shared/index.js';

const event_types = ['create', 'note', 'remove', 'removenote', 'edit'] as const;
const event_types = [
'create',
'note',
'remove',
'removenote',
'edit',
'move',
] as const;
type AnnounceEvent = (typeof event_types)[any];

interface WebhookEmbed {
Expand All @@ -27,16 +34,22 @@ export function onAnnounceEvent(ev: AnnounceEvent, entry: Entry, note?: Note) {
const action = {
create: 'created',
note: 'noted on',
edit: 'edited',
remove: 'removed',
removenote: 'removed a note on',
edit: 'edited',
move: 'moved',
}[ev];
if (!action) {
console.log(`!! unexpected action ${action} in announce.entry`);
return;
}

const scope = entry.scope !== 'en' ? ` in scope __${entry.scope}__` : '';
const scope =
ev === 'move'
? ` to scope __${entry.scope}__`
: entry.scope !== 'en'
? ` in scope __${entry.scope}__`
: '';
const title = note
? `*${note.user}* ${action} **${entry.head}**`
: `*${entry.user}* ${action} **${entry.head}**${scope}`;
Expand Down

0 comments on commit 6aae8f6

Please sign in to comment.