Skip to content

Commit d7df4c0

Browse files
committed
fix(character-count): use the NcActionTexts name prop
The default slot somehow is not reactive. Signed-off-by: Max <max@nextcloud.com>
1 parent 2831462 commit d7df4c0

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

src/components/Menu/CharacterCount.vue

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
-->
55

66
<template>
7-
<NcActionText data-text-action-entry="character-count">
7+
<NcActionText data-text-action-entry="character-count" :name="countString">
88
<template #icon>
99
<AlphabeticalVariant />
1010
</template>
11-
<template #default>
12-
{{ countString }}
13-
</template>
1411
</NcActionText>
1512
</template>
1613

1714
<script>
18-
import { defineComponent } from 'vue'
15+
import { defineComponent, ref } from 'vue'
1916
import { translatePlural as n } from '@nextcloud/l10n'
2017
import NcActionText from '@nextcloud/vue/components/NcActionText'
2118
import { AlphabeticalVariant } from '../icons.js'
@@ -32,37 +29,29 @@ export default defineComponent({
3229
},
3330
setup() {
3431
const { editor } = useEditor()
35-
return { editor }
36-
},
37-
data: () => ({
38-
wordCount: 0,
39-
charCount: 0,
40-
}),
41-
computed: {
42-
countString() {
43-
return `${n('text', '%n word', '%n words', this.wordCount)}, ${n('text', '%n char', '%n chars', this.charCount)}`
44-
},
32+
const countString = ref('')
33+
const refresh = () => {
34+
if (!editor.value) {
35+
return
36+
}
37+
const { storage, state } = editor.value
38+
// characterCount is not reactive so we need this workaround
39+
// We also need to provide the doc as storage is a singleton in tiptap v2.
40+
// See ueberdosis/tiptap#6060
41+
const wordCount = storage.characterCount.words({ node: state.doc })
42+
const charCount = storage.characterCount.characters({ node: state.doc })
43+
const words = n('text', '%n word', '%n words', wordCount)
44+
const chars = n('text', '%n char', '%n chars', charCount)
45+
countString.value = [words, chars].join(', ')
46+
console.debug({ wordCount, charCount, countString: countString.value })
47+
}
48+
return { countString, editor, refresh }
4549
},
4650
watch: {
4751
visible: 'refresh',
4852
},
4953
created() {
5054
this.refresh()
5155
},
52-
methods: {
53-
refresh() {
54-
if (!this.editor) {
55-
this.wordCount = 0
56-
this.charCount = 0
57-
return
58-
}
59-
const { storage, state } = this.editor
60-
// characterCount is not reactive so we need this workaround
61-
// We also need to provide the doc as storage is a singleton in tiptap v2.
62-
// See ueberdosis/tiptap#6060
63-
this.wordCount = storage.characterCount.words(state.doc)
64-
this.charCount = storage.characterCount.characters(state.doc)
65-
},
66-
},
6756
})
6857
</script>

0 commit comments

Comments
 (0)