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

Add submit, multiline and maxlength support #1465

Merged
merged 4 commits into from
Oct 14, 2020
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
6 changes: 5 additions & 1 deletion l10n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ msgstr ""
msgid "Frequently used"
msgstr ""

#: src/components/RichContenteditable/RichContenteditable.vue:254
msgid "Message limit of {count} characters reached"
msgstr ""

#: src/components/Modal/Modal.vue:166
msgid "Next"
msgstr ""
Expand Down Expand Up @@ -120,6 +124,6 @@ msgstr ""
msgid "Unable to search the group"
msgstr ""

#: src/components/RichContenteditable/RichContenteditable.vue:126
#: src/components/RichContenteditable/RichContenteditable.vue:151
msgid "Write message, @ to mention someone …"
msgstr ""
8 changes: 2 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"linkifyjs": "~2.1.9",
"md5": "^2.2.1",
"regenerator-runtime": "^0.13.5",
"string-length": "^4.0.1",
"striptags": "^3.1.1",
"tributejs": "^5.1.3",
"v-click-outside": "^3.0.1",
Expand Down
14 changes: 12 additions & 2 deletions src/components/RichContenteditable/AutoCompleteResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export default {

<style lang="scss" scoped>
@import '../../fonts/scss/iconfont-vue';

$autocomplete-padding: 10px;

.autocomplete-result {
Expand All @@ -118,7 +117,9 @@ $autocomplete-padding: 10px;

&__icon {
position: relative;
flex: 0 0 $clickable-area;
width: $clickable-area;
min-width: $clickable-area;
height: $clickable-area;
border-radius: $clickable-area;
background-color: var(--color-background-darker);
Expand Down Expand Up @@ -168,14 +169,23 @@ $autocomplete-padding: 10px;

&__content {
display: flex;
flex: 1 1;
flex: 1 1 100%;
flex-direction: column;
justify-content: center;
min-width: 0;
padding-left: $autocomplete-padding;
}

&__title,
&__subline {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

&__subline {
color: var(--color-text-lighter);
}
}

</style>
117 changes: 113 additions & 4 deletions src/components/RichContenteditable/RichContenteditable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,31 @@ This component displays contenteditable div with automated @ autocompletion [at]
<RichContenteditable
v-model="message"
:auto-complete="autoComplete"
:maxlength="100"
:user-data="userData"
placeholder="Try mentioning the user Test01"
style="min-height: 100px;" />
@submit="onSubmit" />
<br>

<RichContenteditable
v-model="message"
:auto-complete="autoComplete"
:maxlength="400"
:multiline="true"
:user-data="userData"
placeholder="Try mentioning the user Test01"
@submit="onSubmit" />
<br>
<br>
{{ JSON.stringify(message) }}
</div>
</template>
<script>
export default {
data() {
return {
message: '',
message: 'Lorem ipsum dolor sit amet.',

// You need to provide this for the inline
// mention to understand what to display or not.
userData: {
Expand Down Expand Up @@ -82,6 +96,9 @@ export default {
*/
autoComplete(search, callback) {
callback(Object.values(this.userData))
},
onSubmit() {
alert(this.message)
}
}
}
Expand All @@ -92,20 +109,28 @@ export default {

<template>
<div ref="contenteditable"
:class="{'rich-contenteditable__input--empty': isEmptyValue}"
v-tooltip="tooltip"
:class="{
'rich-contenteditable__input--empty': isEmptyValue,
'rich-contenteditable__input--multiline': multiLine,
'rich-contenteditable__input--overflow': isOverMaxlength,
}"
:contenteditable="contenteditable"
:placeholder="placeholder"
aria-multiline="true"
danxuliu marked this conversation as resolved.
Show resolved Hide resolved
class="rich-contenteditable__input"
role="textbox"
@input="onInput"
@keydown.delete="onDelete"
@keydown.enter.exact="onEnter"
@keydown.ctrl.enter.exact.stop.prevent="onCtrlEnter"
@paste="onPaste" />
</template>

<script>
import Tribute from 'tributejs/dist/tribute.esm'
import debounce from 'debounce'
import stringLength from 'string-length'

import { t } from '../../l10n.js'
import AutoCompleteResult from './AutoCompleteResult'
Expand Down Expand Up @@ -133,13 +158,34 @@ export default {
type: Element,
default: () => document.body,
},

/**
* Make the contenteditable looks like a textarea or not.
* Default looks like a single-line input.
* This also handle the default enter/shift+enter behaviour.
* if multiLine, enter = newline; otherwise enter = submit
* shift+enter always add a new line. ctrl+enter always submits
*/
multiLine: {
type: Boolean,
default: false,
},

/**
* Is the content editable
* Is the content editable ?
*/
contenteditable: {
type: Boolean,
default: true,
},

/**
* Max allowed length
*/
maxlength: {
type: Number,
default: null,
},
},

data() {
Expand Down Expand Up @@ -169,6 +215,10 @@ export default {
},

computed: {
/**
* Is the current trimmed value empty?
* @returns {boolean}
*/
isEmptyValue() {
return !this.localValue || this.localValue.trim() === ''
},
Expand All @@ -181,6 +231,31 @@ export default {
return !!navigator.userAgent.match(/firefox/i)
},

/**
* Is the current value over maxlength?
* @returns {boolean}
*/
isOverMaxlength() {
if (this.isEmptyValue || !this.maxlength) {
return false
}
return stringLength(this.localValue) > this.maxlength
},

/**
* Tooltip to show if characters count is over limit
* @returns {string}
*/
tooltip() {
if (!this.isOverMaxlength) {
return null
}
return {
content: t('Message limit of {count} characters reached', { count: this.maxlength }),
show: true,
trigger: 'manual',
}
},
},

watch: {
Expand Down Expand Up @@ -326,6 +401,32 @@ export default {
}
},

/**
* Enter key pressed. Submits if not multiline
* @param {Event} event the keydown event
*/
onEnter(event) {
// Prevent submitting if autocompletion menu
// is opened or length is over maxlength
if (this.multiLine || this.isOverMaxlength || this.tribute.isActive) {
return
}

event.preventDefault()
event.stopPropagation()
this.$emit('submit', event)
},
/**
* Ctrl + Enter key pressed
* @param {Event} event the keydown event
*/
onCtrlEnter(event) {
if (this.isOverMaxlength) {
return
}
this.$emit('submit', event)
},

/**
* Debounce the autocomplete function
*/
Expand Down Expand Up @@ -353,6 +454,8 @@ export default {
background-color: var(--color-main-background);
font-family: var(--font-face);
font-size: inherit;
min-height: $clickable-area;
max-height: $clickable-area * 5.5;

// Cannot use :empty because of firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=1513303
&--empty:before {
Expand All @@ -369,6 +472,12 @@ export default {
border-radius: var(--border-radius);
background-color: var(--color-background-dark);
}

&--multiline {
min-height: $clickable-area * 3;
// No max for mutiline
max-height: none;
}
}

</style>
Expand Down