-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: AI dialog box, left mouse button menu #2005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<template> | ||
<div> | ||
<vue3-menus v-model:open="isOpen" :event="eventVal" :menus="menus" hasIcon> | ||
<template #icon="{ menu }" | ||
><AppIcon v-if="menu.icon" :iconName="menu.icon"></AppIcon | ||
></template> | ||
<template #label="{ menu }"> {{ menu.label }}</template> | ||
</vue3-menus> | ||
</div> | ||
</template> | ||
<script setup lang="ts"> | ||
import { Vue3Menus } from 'vue3-menus' | ||
import { MsgSuccess } from '@/utils/message' | ||
import AppIcon from '@/components/icons/AppIcon.vue' | ||
import bus from '@/bus' | ||
import { ref, nextTick, onMounted } from 'vue' | ||
const isOpen = ref<boolean>(false) | ||
const eventVal = ref({}) | ||
function getSelection() { | ||
const selection = window.getSelection() | ||
if (selection && selection.anchorNode == null) { | ||
return null | ||
} | ||
const text = selection?.anchorNode?.textContent | ||
return text && text.substring(selection.anchorOffset, selection.focusOffset) | ||
} | ||
/** | ||
* 打开控制台 | ||
* @param event | ||
*/ | ||
const openControl = (event: any) => { | ||
const c = getSelection() | ||
isOpen.value = false | ||
if (c) { | ||
nextTick(() => { | ||
eventVal.value = event | ||
isOpen.value = true | ||
}) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
const menus = ref([ | ||
{ | ||
label: '复制', | ||
icon: 'app-copy', | ||
click: () => { | ||
const selectionText = getSelection() | ||
if (selectionText) { | ||
clearSelectedText() | ||
navigator.clipboard.writeText(selectionText).then(() => { | ||
MsgSuccess('复制成功') | ||
}) | ||
} | ||
} | ||
}, | ||
{ | ||
label: '引用', | ||
icon: 'app-quote', | ||
click: () => { | ||
bus.emit('chat-input', getSelection()) | ||
clearSelectedText() | ||
} | ||
} | ||
]) | ||
/** | ||
* 清除选中文本 | ||
*/ | ||
const clearSelectedText = () => { | ||
if (window.getSelection) { | ||
var selection = window.getSelection() | ||
if (selection) { | ||
selection.removeAllRanges() | ||
} | ||
} | ||
} | ||
onMounted(() => { | ||
bus.on('open-control', openControl) | ||
}) | ||
</script> | ||
<style lang="scss"></style> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks mostly clean and well-structured, but there are a few things that could be improved:
Here's the revised version with these improvements: @@ -0,0 +1,86 @@
+<template>
+ <div>
+ <vue3-menus v-model:open="isOpen" :event="evenVal" :menus="menus" has-icon> <!-- Changed ':event' to 'v-on:event=' -->
+ <template #icon="{ menu }">
+ <AppIcon v-if="menu.icon" :icon-name="menu.icon"></AppIcon>
+ </template>
+ <template #label="{ menu }">{{ menu.label }}</template>
+ </vue3-menus>
+ </div>
+</template>
+
<script setup lang="ts">
+import { ref, watchEffect, onMounted } from 'vue';
+import Vue3Menus from 'vue3-menus';
+import { MsgSuccess } from '@/utils/message';
+import AppIcon from '@/components/icons/AppIcon.vue';
+import bus from '@/bus';
interface MenuItem {
label: string;
icon?: string;
click?(): void;
}
const isOpen = ref(false);
const evenVal = ref({}); // Corrected ':event' to 'v-on:event='
function getSelection() {
const selection = window.getSelection();
if (!selection || !selection.anchorNode) {
return null;
}
const text = selection.ancherNode.textContent;
return text && text.substring(selection.anchor_offset, selection.focus_offset);
}
/**
* 打开控制台
* @param event
*/
const openControl = async (event: any) => {
const c = getSelection();
isOpen.value = false;
if (c) {
try {
await nextTick(); // Wait until DOM is updated
evenVal.value = event;
isOpen.value = true;
} catch (error) {
console.error('Error opening control:', error);
}
event.preventDefault();
}
};
watchEffect(() => {
if (window.navigator.clipboard && window.navigator.permissions) {
bus.on(
'open-control',
(evt: Event, selectionText: string | null, clipboardSupported: boolean) => {
if (clipboardSupported && selectionText !== null) {
clearSelectedText();
window.navigator.clipboard.writeText(selectionText).then(
() => {
MsgSuccess('复制成功');
},
(err) => {
console.error('Error copying:', err);
}
);
}
}
);
} else {
bus.off('open-control');
}
});
function clearSelectedText() {
if (document.getSelection) {
let selection = document.getSelection();
if (selection && selection.type === 'Range') {
selection.removeAllRanges();
}
}
}
onMounted(async () => {
const clipboardSupported = await window.navigator.permissions.query({ name: "clipboard-write" }).catch(console.log);
const initialDataForBus = clipboardSupported.state === "granted"
? [null, "", clipboardSupported]
: [];
bus.emit("initial-data", ...initialDataForBus); // Emit appropriate data based on permission state
});
</script>
<style scoped></style> Key Changes Made:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1374,4 +1374,25 @@ export const iconMap: any = { | |
]) | ||
} | ||
}, | ||
'app-quote': { | ||
iconReader: () => { | ||
return h('i', [ | ||
h( | ||
'svg', | ||
{ | ||
style: { height: '100%', width: '100%' }, | ||
viewBox: '0 0 1024 1024', | ||
version: '1.1', | ||
xmlns: 'http://www.w3.org/2000/svg' | ||
}, | ||
[ | ||
h('path', { | ||
d: 'M800.768 477.184c-14.336 0-30.72 2.048-45.056 4.096 18.432-51.2 77.824-188.416 237.568-315.392 36.864-28.672-20.48-86.016-59.392-57.344-155.648 116.736-356.352 317.44-356.352 573.44v20.48c0 122.88 100.352 223.232 223.232 223.232S1024 825.344 1024 702.464c0-124.928-100.352-225.28-223.232-225.28zM223.232 477.184c-14.336 0-30.72 2.048-45.056 4.096 18.432-51.2 77.824-188.416 237.568-315.392 36.864-28.672-20.48-86.016-59.392-57.344C200.704 225.28 0 425.984 0 681.984v20.48c0 122.88 100.352 223.232 223.232 223.232s223.232-100.352 223.232-223.232c0-124.928-100.352-225.28-223.232-225.28z', | ||
fill: 'currentColor' | ||
}) | ||
] | ||
) | ||
]) | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code snippet is correct and does not have any significant irregularities or issues. The However, there are a couple of general tips for maintaining clean and optimized code:
Overall, this update seems well-intentioned and should function properly with minimal adjustments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code snippet seems to be related to an interactive component that displays information about an application and provides a way for users to interact with it (by clicking on the card).
Potential Issues and Optimization Suggestions:
Event Handling:
@stop
directive applied to the.content
div, which prevents any further event propagation up to parent elements. This can make sure that clicks within this element don't trigger unnecessary actions outside of its boundary.Optimization:
computed()
is generally not recommended because it means the function is recreated whenever anything changes around it. You might consider using an arrow function instead if you want the function to have access to thethis
context defined in another scope.Code Duplication:
add_answer_text_list()
function duplicates some logic from withinanswer_text_list()
. Consider merging these functions or extracting common functionality into separate utility methods.Here's the cleaned-up code:
Explanation:
chatMessage
andhandleCardClick
.This version maintains readability while improving performance by minimizing redundant code execution.