Skip to content
Open
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
58 changes: 22 additions & 36 deletions packages/core/src/components/Sender/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const props = withDefaults(defineProps<SenderProps>(), {
minRows: 1,
maxRows: 6
}),
minlength: undefined,
maxlength: undefined,
submitType: 'enter',
headerAnimationTimer: 300,
inputWidth: '100%',
Expand Down Expand Up @@ -56,8 +58,7 @@ const internalValue = computed({
return props.modelValue;
},
set(val) {
if (props.readOnly || props.disabled)
return;
if (props.readOnly || props.disabled) return;
emits('update:modelValue', val);
}
});
Expand Down Expand Up @@ -87,8 +88,7 @@ const popoverVisible = computed({
return props.triggerPopoverVisible;
},
set(value) {
if (props.readOnly || props.disabled)
return;
if (props.readOnly || props.disabled) return;
emits('update:triggerPopoverVisible', value);
}
});
Expand All @@ -100,8 +100,7 @@ const triggerString = ref('');
watch(
() => internalValue.value,
(newVal, oldVal) => {
if (isComposing.value)
return;
if (isComposing.value) return;
// 触发逻辑:当输入值等于数组中的任意一个指令字符时触发
// 确保 oldVal 是字符串类型
const triggerStrings = props.triggerStrings || []; // 如果为 undefined,就使用空数组
Expand All @@ -120,8 +119,7 @@ watch(
isOpen: true
});
popoverVisible.value = true;
}
else {
} else {
popoverVisible.value = true;
}
}
Expand All @@ -135,8 +133,7 @@ watch(
isOpen: false
});
popoverVisible.value = false;
}
else {
} else {
popoverVisible.value = false;
}
}
Expand All @@ -151,8 +148,7 @@ watch(
isOpen: true
});
popoverVisible.value = true;
}
else {
} else {
popoverVisible.value = true;
}
}
Expand All @@ -173,19 +169,15 @@ function onContentMouseDown(e: MouseEvent) {
/* 头部显示隐藏 开始 */
const visiableHeader = ref(false);
function openHeader() {
if (!slots.header)
return false;
if (!slots.header) return false;

if (props.readOnly)
return false;
if (props.readOnly) return false;

visiableHeader.value = true;
}
function closeHeader() {
if (!slots.header)
return;
if (props.readOnly)
return;
if (!slots.header) return;
if (props.readOnly) return;
visiableHeader.value = false;
}
/* 头部显示隐藏 结束 */
Expand All @@ -195,8 +187,7 @@ const recognition = ref<SpeechRecognition | null>(null);
const speechLoading = ref<boolean>(false);

function startRecognition() {
if (props.readOnly)
return; // 直接返回,不执行后续逻辑
if (props.readOnly) return; // 直接返回,不执行后续逻辑
if (hasOnRecordingChangeListener.value) {
speechLoading.value = true;
emits('recordingChange', true);
Expand Down Expand Up @@ -227,8 +218,7 @@ function startRecognition() {
speechLoading.value = false;
};
recognition.value.start();
}
else {
} else {
console.error('浏览器不支持 Web Speech API');
}
}
Expand Down Expand Up @@ -261,22 +251,19 @@ function submit() {
}
// 取消按钮
function cancel() {
if (props.readOnly)
return;
if (props.readOnly) return;
emits('cancel', internalValue.value);
}

function clear() {
if (props.readOnly)
return; // 直接返回,不执行后续逻辑
if (props.readOnly) return; // 直接返回,不执行后续逻辑
inputRef.value.clear();
internalValue.value = '';
}

// 在这判断组合键的回车键 (目前支持四种模式)
function handleKeyDown(e: { target: HTMLTextAreaElement } & KeyboardEvent) {
if (props.readOnly)
return; // 直接返回,不执行后续逻辑
if (props.readOnly) return; // 直接返回,不执行后续逻辑
const _resetSelectionRange = () => {
const cursorPosition = e.target.selectionStart; // 获取光标位置
const textBeforeCursor = internalValue.value.slice(0, cursorPosition); // 光标前的文本
Expand Down Expand Up @@ -307,8 +294,7 @@ function handleKeyDown(e: { target: HTMLTextAreaElement } & KeyboardEvent) {
e.preventDefault();
if (props.submitType === 'enter') {
_isComKeyDown ? _resetSelectionRange() : submit();
}
else {
} else {
_isComKeyDown ? submit() : _resetSelectionRange();
}
}
Expand All @@ -329,11 +315,9 @@ function focus(type = 'all') {
}
if (type === 'all') {
inputRef.value.select();
}
else if (type === 'start') {
} else if (type === 'start') {
focusToStart();
}
else if (type === 'end') {
} else if (type === 'end') {
focusToEnd();
}
}
Expand Down Expand Up @@ -458,6 +442,8 @@ defineExpose({
:rows="1"
:autosize="autoSize"
type="textarea"
:minlength="minlength"
:maxlength="maxlength"
:validate-event="false"
:placeholder="placeholder"
:read-only="readOnly || disabled"
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/components/Sender/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface SenderProps {
minRows: number;
maxRows: number;
};
// 原生属性,最小输入长度
minlength?: string | number;
// 同原生 maxlength 属性
maxlength?: string | number;
readOnly?: boolean;
disabled?: boolean;
loading?: boolean;
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/stories/Sender/Sender.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const meta: Meta<typeof SenderSource> = {
control: 'object',
description: '设置输入框的最小展示行数和最大展示行数。'
},
minlength: {
defaultValue: 0,
control: 'number',
description: '设置输入框的最小输入长度。'
},
maxlength: {
defaultValue: 10,
control: 'number',
description: '设置输入框的最大输入长度。'
},
readOnly: {
defaultValue: false,
control: 'boolean',
Expand Down Expand Up @@ -139,6 +149,8 @@ const meta: Meta<typeof SenderSource> = {
minRows: 1,
maxRows: 5
},
minlength: 0,
maxlength: 10,
readOnly: false,
disabled: false,
submitBtnDisabled: false,
Expand Down