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

feat(NcRichContenteditable): programmatically show tributes #5674

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
66 changes: 66 additions & 0 deletions src/components/NcRichContenteditable/NcRichContenteditable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,48 @@ export default {
</style>
```

### Using public methods

```vue
<template>
<div>
<div class="buttons-wrapper">
<NcButton class="show-slash-button" @click="showSlashCommands">Slash commands</NcButton>
<NcButton class="focus-button" @click="focus">Focus on input</NcButton>
</div>
<NcRichContenteditable
ref="contenteditable"
label="Write a comment"
:value.sync="message"
:maxlength="100"/>
</div>
</template>
<script>
export default {
data() {
return {
message: '**Lorem ipsum** dolor sit amet. ',
}
},
methods: {
showSlashCommands() {
this.$refs.contenteditable.showTribute('/')
},
focus() {
this.$refs.contenteditable.focus()
},
},
}
</script>
<style lang="scss" scoped>
.buttons-wrapper {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
</style>
```

</docs>

<template>
Expand Down Expand Up @@ -922,6 +964,8 @@ export default {
this.getTributeContainer().setAttribute('class', this.tribute.current.collection.containerClass || this.$style['tribute-container'])

this.setupTributeIntegration()
// Remove the event handler if any
document.removeEventListener('click', this.hideTribute, true)
} else {
// Cancel loading data for autocomplete
// Otherwise it could be received when another autocomplete is already opened
Expand Down Expand Up @@ -1001,6 +1045,28 @@ export default {
this.getTributeContainer().classList.remove(this.$style['tribute-container--focus-visible'])
}
},

/**
* Show tribute menu programmatically.
* @param {string} trigger - trigger character, can be '/', '@', or ':'
*
* @public
*/
showTribute(trigger) {
this.focus()
const index = this.tribute.collection.findIndex(collection => collection.trigger === trigger)
this.tribute.showMenuForCollection(this.$refs.contenteditable, index)
document.addEventListener('click', this.hideTribute, true)
},

/**
* Hide tribute menu programmatically
*
*/
hideTribute() {
this.tribute.hideMenu()
document.removeEventListener('click', this.hideTribute, true)
},
},
}
</script>
Expand Down
Loading