Skip to content

Commit 0b7fa68

Browse files
committed
feat: 增加双击消息编辑功能
1 parent 8f516b5 commit 0b7fa68

3 files changed

Lines changed: 95 additions & 19 deletions

File tree

src/components/common/MessageItem.vue

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,30 @@
6262
</span>
6363
</div>
6464

65-
<div class="message-content" :style="computedStyles.contentStyle">
66-
{{ message.content }}
65+
<div
66+
class="message-content"
67+
:style="computedStyles.contentStyle"
68+
@dblclick="startEditing"
69+
>
70+
<template v-if="isEditing">
71+
<textarea
72+
ref="editInput"
73+
v-model="editContent"
74+
class="content-editor"
75+
@blur="saveEdit"
76+
@keydown.enter.ctrl="saveEdit"
77+
@keydown.esc="cancelEdit"
78+
></textarea>
79+
</template>
80+
<template v-else>
81+
{{ message.content }}
82+
</template>
6783
</div>
6884
</div>
6985
</template>
7086

7187
<script setup lang="ts">
72-
import { computed } from 'vue';
88+
import { computed, ref, nextTick } from 'vue';
7389
import type { Message } from '@/types/log';
7490
import { useUiStore } from '@/stores/uiStore';
7591
import { useStyleStore } from '@/stores/styleStore';
@@ -102,6 +118,7 @@ const emit = defineEmits<{
102118
(e: 'actionMerge'): void;
103119
(e: 'actionSplit'): void;
104120
(e: 'actionDelete'): void;
121+
(e: 'updateContent', content: string): void;
105122
}>();
106123
107124
const styleStore = useStyleStore();
@@ -145,6 +162,30 @@ const isHidden = computed(() => {
145162
(hideCommand && props.message.isCommand)
146163
);
147164
});
165+
166+
const isEditing = ref(false);
167+
const editContent = ref('');
168+
const editInput = ref<HTMLTextAreaElement | null>(null);
169+
170+
function startEditing() {
171+
editContent.value = props.message.content;
172+
isEditing.value = true;
173+
nextTick(() => {
174+
editInput.value?.focus();
175+
});
176+
}
177+
178+
function saveEdit() {
179+
if (!isEditing.value) return;
180+
if (editContent.value !== props.message.content) {
181+
emit('updateContent', editContent.value);
182+
}
183+
isEditing.value = false;
184+
}
185+
186+
function cancelEdit() {
187+
isEditing.value = false;
188+
}
148189
</script>
149190

150191
<style scoped>
@@ -245,4 +286,19 @@ const isHidden = computed(() => {
245286
width: 14px;
246287
height: 14px;
247288
}
289+
290+
.content-editor {
291+
width: 100%;
292+
min-height: 3em;
293+
padding: 4px 8px;
294+
font-family: inherit;
295+
font-size: inherit;
296+
line-height: inherit;
297+
border: 1px solid var(--active-accent);
298+
border-radius: 4px;
299+
background: var(--bg-input);
300+
color: var(--text-main);
301+
resize: vertical;
302+
outline: none;
303+
}
248304
</style>

src/components/workspace/ChunkView.vue

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
@action-merge="handleActionMerge(msg)"
5151
@action-split="handleActionSplit(msg.messageId)"
5252
@action-delete="handleActionDelete(msg.messageId)"
53+
@update-content="
54+
(newContent) =>
55+
handleUpdateContent(msg.messageId, newContent)
56+
"
5357
/>
5458
</div>
5559
</template>
@@ -74,7 +78,6 @@ import { useMessageEditorStore } from '@/stores/editorStore/messageStore';
7478
import { useChunkEditorStore } from '@/stores/editorStore/chunkStore';
7579
import { useCommandDispatcher } from '@/composables/useCommandDispatcher';
7680
import { useWindowStore } from '@/stores/windowStore';
77-
import { generateId } from '@/utils/id';
7881
import type { Message } from '@/types/log';
7982
8083
const props = defineProps<{ chunkId: string }>();
@@ -115,6 +118,12 @@ function handleMessageSelect(event: MouseEvent, msgId: string, index: number) {
115118
});
116119
}
117120
121+
function handleUpdateContent(messageId: string, newContent: string) {
122+
messageEditorStore.updateMessage(props.chunkId, messageId, {
123+
content: newContent,
124+
});
125+
}
126+
118127
function handleMessageDragStart(
119128
event: DragEvent,
120129
messageId: string,
@@ -159,26 +168,12 @@ function handleContainerDrop(event: DragEvent) {
159168
}
160169
161170
function handleActionInsert(msg: Message, index: number) {
162-
const newMessage: Message = {
163-
messageId: generateId(),
164-
chunkId: props.chunkId,
165-
messageIndex: index + 1,
166-
playerName: msg.playerName,
167-
account: msg.account,
168-
time: new Date(),
169-
content: '',
170-
isOoc: false,
171-
isCommand: false,
172-
role: msg.role,
173-
note: '',
174-
};
175-
messageEditorStore.addMessage(props.chunkId, newMessage, index + 1);
171+
messageEditorStore.insertNewMessageAfter(props.chunkId, msg, index);
176172
}
177173
178174
function handleActionMerge(msg: Message) {
179175
const selectedIds = filterTool.selectedMessageIds.value;
180176
if (selectedIds.has(msg.messageId) && selectedIds.size > 1) {
181-
// 多选状态合并
182177
messageEditorStore.mergeMessages(
183178
props.chunkId,
184179
Array.from(selectedIds),

src/stores/editorStore/messageStore.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia';
22
import { useLogStore } from '@/stores/logStore';
33
import { useHistoryStore } from '@/stores/historyStore';
44
import type { Message, Chunk } from '@/types/log';
5+
import { generateId } from '@/utils/id';
56

67
export const useMessageEditorStore = defineStore('messageEditor', () => {
78
const logStore = useLogStore();
@@ -46,6 +47,29 @@ export const useMessageEditorStore = defineStore('messageEditor', () => {
4647
refreshMessageMetadata(chunk);
4748
}
4849

50+
function insertNewMessageAfter(
51+
chunkId: string,
52+
msg: Message,
53+
index: number,
54+
) {
55+
const newMessage: Message = {
56+
messageId: generateId(),
57+
chunkId: chunkId,
58+
messageIndex: index + 1,
59+
playerName: msg.playerName,
60+
account: msg.account,
61+
time: new Date(),
62+
content: '',
63+
isOoc: false,
64+
isCommand: false,
65+
role: msg.role,
66+
note: '',
67+
};
68+
69+
// 调用现有的通用插入逻辑
70+
insertMessages(chunkId, [newMessage], index + 1);
71+
}
72+
4973
function deleteMessage(chunkId: string, messageId: string) {
5074
const chunk = logStore.findChunkById(chunkId);
5175
if (!chunk) return;
@@ -285,6 +309,7 @@ export const useMessageEditorStore = defineStore('messageEditor', () => {
285309
return {
286310
addMessage,
287311
insertMessages,
312+
insertNewMessageAfter,
288313
deleteMessage,
289314
updateMessage,
290315
moveMessages,

0 commit comments

Comments
 (0)