Skip to content
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

Set char limit on rich text #662

Merged
merged 3 commits into from
Jan 14, 2025
Merged
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
28 changes: 25 additions & 3 deletions client/components/forms/RichTextAreaInput.client.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,33 @@
v-model="compVal"
:options="quillOptions"
:disabled="disabled"
:placeholder="placeholder"
:style="inputStyle"
/>
</div>

<template #help>
<template
v-if="$slots.help"
#help
>
<slot name="help" />
</template>
<template #error>

<template
v-if="maxCharLimit && showCharLimit"
#bottom_after_help
>
<small :class="theme.default.help">
{{ charCount }}/{{ maxCharLimit }}
</small>
</template>

<template
v-if="$slots.error"
#error
>
<slot name="error" />
</template>

Comment on lines +37 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Prevent input when character limit is exceeded.

Currently, the component only displays the character count but doesn't prevent users from exceeding the limit. Consider adding an input prevention mechanism:

 <QuillyEditor
   :id="id ? id : name"
   ref="editor"
   v-model="compVal"
   :options="quillOptions"
   :disabled="disabled"
   :placeholder="placeholder"
   :style="inputStyle"
+  @text-change="handleTextChange"
 />

+const handleTextChange = (delta, oldDelta, source) => {
+  if (source === 'user' && props.maxCharLimit && charCount.value > props.maxCharLimit) {
+    // Revert the change
+    editor.value.quill.history.undo();
+  }
+};

Committable suggestion skipped: line range outside the PR's diff.

<MentionDropdown
v-if="enableMentions && mentionState"
:state="mentionState"
Expand All @@ -59,6 +75,7 @@ import registerMentionExtension from '~/lib/quill/quillMentionExtension.js'

const props = defineProps({
...inputProps,
maxCharLimit: { type: Number, required: false, default: null },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for maxCharLimit prop.

The maxCharLimit prop should validate:

  • Non-negative numbers
  • Reasonable maximum value to prevent performance issues
-  maxCharLimit: { type: Number, required: false, default: null },
+  maxCharLimit: {
+    type: Number,
+    required: false,
+    default: null,
+    validator: (value) => {
+      return value === null || (value > 0 && value <= 100000);
+    }
+  },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
maxCharLimit: { type: Number, required: false, default: null },
maxCharLimit: {
type: Number,
required: false,
default: null,
validator: (value) => {
return value === null || (value > 0 && value <= 100000);
}
},

editorOptions: {
type: Object,
default: () => ({})
Expand Down Expand Up @@ -86,6 +103,7 @@ if (props.enableMentions && !Quill.imports['blots/mention']) {

const quillOptions = computed(() => {
const defaultOptions = {
placeholder: props.placeholder || '',
theme: 'snow',
modules: {
toolbar: [
Expand All @@ -110,6 +128,10 @@ const quillOptions = computed(() => {

return mergedOptions
})

const charCount = computed(() => {
return compVal.value ? compVal.value.replace(/<[^>]*>/g, '').trim().length : 0
})
</script>

<style lang="scss">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@
@update:model-value="onFieldHelpPositionChange"
/>

<template v-if="['text', 'number', 'url', 'email'].includes(field.type)">
<template v-if="['text', 'rich_text', 'number', 'url', 'email'].includes(field.type)">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Missing prop definition and character limit enforcement in RichTextAreaInput

  • showCharLimit is used in template but not defined as a prop
  • Character limit is displayed but not enforced through validation or input restriction
🔗 Analysis chain

Verify integration with RichTextAreaInput component.

The changes look good, but we should verify that the RichTextAreaInput component correctly handles these character limit properties.

Also applies to: 844-846

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that RichTextAreaInput component handles maxCharLimit and showCharLimit props

# Search for RichTextAreaInput component definition
echo "Searching for RichTextAreaInput component definition..."
ast-grep --pattern 'export default {
  name: "RichTextAreaInput",
  $$$
}'

# Search for maxCharLimit and showCharLimit prop definitions
echo "Searching for character limit prop definitions..."
rg -A 5 'props.*\{' client/components/**/RichTextAreaInput*.vue

# Search for character limit usage in template
echo "Searching for character limit usage in template..."
rg -A 5 'maxCharLimit|showCharLimit' client/components/**/RichTextAreaInput*.vue

Length of output: 1895


Script:

#!/bin/bash
# Search for showCharLimit prop and charCount implementation
echo "Searching for showCharLimit prop..."
rg -A 2 'showCharLimit.*\{' client/components/forms/RichTextAreaInput.client.vue

echo "Searching for charCount implementation..."
rg -A 5 'charCount' client/components/forms/RichTextAreaInput.client.vue

echo "Searching for validation or restriction logic..."
rg -A 10 'maxCharLimit' client/components/forms/RichTextAreaInput.client.vue

Length of output: 1305

<text-input
name="max_char_limit"
native-type="number"
Expand Down Expand Up @@ -841,6 +841,9 @@ export default {
multi_lines: false,
max_char_limit: 2000
},
rich_text: {
max_char_limit: 2000
},
email: {
max_char_limit: 2000
},
Expand Down
Loading