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

Stop TextTruncator from briefly showing full text before shave.js truncates it #8101

Merged
merged 2 commits into from
May 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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