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

fix: Use resize observer to calculate menubar icon limit #4637

Merged
merged 2 commits into from
Aug 7, 2023
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
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-editors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-editors.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/vendors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/vendors.js.map

Large diffs are not rendered by default.

93 changes: 77 additions & 16 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@tiptap/pm": "^2.0.4",
"@tiptap/suggestion": "^2.0.4",
"@tiptap/vue-2": "^2.0.4",
"@vueuse/shared": "^10.3.0",
"debounce": "^1.2.1",
"escape-html": "^1.0.3",
"highlight.js": "^11.8.0",
Expand Down Expand Up @@ -123,6 +124,7 @@
"@vitejs/plugin-vue2": "^2.2.0",
"@vue/test-utils": "^1.3.0 <2",
"@vue/vue2-jest": "^29.2.4",
"@vueuse/core": "^10.3.0",
"cypress": "^12.17.3",
"eslint-plugin-cypress": "^2.13.3",
"identity-obj-proxy": "^3.0.0",
Expand Down
77 changes: 17 additions & 60 deletions src/components/Menu/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@

<script>
import { NcActionSeparator, NcActionButton } from '@nextcloud/vue'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import debounce from 'debounce'
import { useResizeObserver } from '@vueuse/core'

import HelpModal from '../HelpModal.vue'
import actionsFullEntries from './entries.js'
Expand Down Expand Up @@ -130,33 +130,14 @@ export default {
randomID: `menu-bar-${(Math.ceil((Math.random() * 10000) + 500)).toString(16)}`,
displayHelp: false,
displayTranslate: false,
forceRecompute: 0,
isReady: false,
isVisible: this.$editor.isFocused,
windowWidth: 0,
canTranslate: loadState('text', 'translation_languages', []).length > 0,
resize: null,
iconsLimit: 4,
}
},
computed: {
iconsLimit() {
// just force this computed to run again
// eslint-disable-next-line no-unused-expressions
(
this.forceRecompute,
this.windowWidth
)
const { menubar } = this.$refs
const menuBarWidth = menubar && menubar.clientWidth > 200
? menubar.clientWidth
: 200

// leave some buffer - this is necessary so the bar does not wrap during resizing
const spaceToFill = menuBarWidth - 4
const slots = Math.floor(spaceToFill / 44)

// Leave one slot empty for the three dot menu
return slots - 1
},
visibleEntries() {
const list = [...actionsFullEntries].filter(({ priority }) => {
// if entry do not have priority, we assume it aways will be visible
Expand All @@ -178,9 +159,7 @@ export default {
},
},
mounted() {
window.addEventListener('resize', this.getWindowWidth)
subscribe('files:sidebar:opened', this.redrawAfterTransition)
subscribe('files:sidebar:closed', this.redrawAfterTransition)
this.resize = useResizeObserver(this.$refs.menubar, this.onResize)
juliusknorr marked this conversation as resolved.
Show resolved Hide resolved

this.$onFocusChange = () => {
this.isVisible = this.$editor.isFocused
Expand All @@ -192,51 +171,29 @@ export default {
this.$editor.on('focus', this.$onFocusChange)
this.$editor.on('blur', this.$onBlurChange)

this.$checkInterval = setInterval(() => {
const { menubar } = this.$refs
const isWidthAvailable = (menubar && menubar.clientWidth > 0)

if (this.$isRichEditor && isWidthAvailable) {
this.redrawMenuBar()
}

if (!this.$isRichEditor || isWidthAvailable) {
clearInterval(this.$checkInterval)
}

if (isWidthAvailable || !this.$isRichEditor) {
this.$nextTick(() => {
this.isReady = true
})
}
}, 100)

this.$nextTick(() => {
this.isReady = true
this.$emit('update:loaded', true)
})
},
beforeDestroy() {
window.removeEventListener('resize', this.getWindowWidth)

unsubscribe('files:sidebar:opened', this.redrawAfterTransition)
unsubscribe('files:sidebar:closed', this.redrawAfterTransition)
this.resize?.stop()

this.$editor.off('focus', this.$onFocusChange)
this.$editor.off('blur', this.$onBlurChange)
},
methods: {
getWindowWidth() {
this.windowWidth = document.documentElement.clientWidth
},
redrawAfterTransition() {
// wait for transition to complete (100ms)
setTimeout(this.redrawMenuBar, 110)
},
redrawMenuBar() {
this.$nextTick(() => {
this.getWindowWidth()
this.forceRecompute++
})
onResize(entries) {
const entry = entries[0]
const { width } = entry.contentRect

// leave some buffer - this is necessary so the bar does not wrap during resizing
const spaceToFill = width - 4
const slots = Math.floor(spaceToFill / 44)

// Leave one slot empty for the three dot menu
this.iconsLimit = slots - 1
this.isReady = true
},
showHelp() {
this.displayHelp = true
Expand Down