Skip to content

Commit

Permalink
Merge pull request #8101 from jonboiser/text-truncator-flashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonboiser committed May 25, 2021
2 parents c7e8771 + af26355 commit 85cbd75
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions kolibri/core/assets/src/views/TextTruncator.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>

<div>
<div :style="{ maxHeight: `${maxHeight - 16}px` }" :class="{ truncated: !shaveDone }">
<div v-if="viewAllText">
{{ text }}
</div>
Expand Down Expand Up @@ -53,14 +53,13 @@
data() {
return {
textIsTruncated: false,
shaveDone: false,
viewAllText: false,
};
},
computed: {
currentDimensions() {
return {
text: this.text,
maxHeight: this.maxHeight,
elementWidth: this.elementWidth,
elementHeight: this.elementHeight,
};
Expand All @@ -74,6 +73,9 @@
this.debouncedHandleUpdate();
},
},
beforeDestroy() {
this.debouncedHandleUpdate.cancel();
},
methods: {
titleIsShaved() {
return Boolean(this.$el.querySelector('.js-shave'));
Expand All @@ -88,20 +90,24 @@
return $shaveEl.clientWidth < $shaveEl.scrollWidth;
}
},
updateTitle() {
// Set title attribute as full text if the visible text is truncated
if (this.textIsTruncated && !this.$refs.shaveEl.title) {
this.$refs.shaveEl.setAttribute('title', this.text);
} else if (!this.textIsTruncated && this.$refs.shaveEl.title) {
// Remove if text is fully visible after a resize
this.$refs.shaveEl.removeAttribute('title');
}
},
handleUpdate() {
// TODO make "View Less" disappear when user expands window
// and text isn't truncated any more.
// and text isn't truncated anymore.
shave(this.$refs.shaveEl, this.maxHeight, { spaces: false });
this.$nextTick(() => {
this.$nextTick().then(() => {
this.textIsTruncated = this.titleIsShaved() || this.titleIsOverflowing();
// set title attribute for shaved text but
// skip if a title already exists
if (this.textIsTruncated && !this.$refs.shaveEl.title)
this.$refs.shaveEl.setAttribute('title', this.text);
// if the text is not shaved and a title has been previously set,
// remove it
else if (!this.textIsTruncated && this.$refs.shaveEl.title)
this.$refs.shaveEl.removeAttribute('title');
this.updateTitle();
// Removes temporary truncated styling from main div
this.shaveDone = true;
});
},
},
Expand Down

0 comments on commit 85cbd75

Please sign in to comment.