Skip to content
Open
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
2 changes: 2 additions & 0 deletions resources/css/components/fieldtypes/bard.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
.bard-fixed-toolbar {
z-index: var(--z-index-portal);
position: sticky;
/* Opt out the toolbar of scroll anchoring. Firefox's scroll anchoring adjusts the scroll position to compensate for the perceived layout change, scrolling up to the parent Bard's top. */
overflow-anchor: none;
@apply sm:-top-2;
/* Prevent the sticky toolbar from hitting the very end of container, which causes it to overlap the container's border-radius */
margin-block-end: 8px;
Expand Down
12 changes: 7 additions & 5 deletions resources/js/components/fieldtypes/Fieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import debounce from '@/util/debounce.js';
import props from './props.js';
import emits from './emits.js';
import { publishContextKey } from '@/components/ui';
import { isRef } from 'vue';
import { isRef, markRaw } from 'vue';

export default {
emits,
Expand All @@ -24,15 +24,17 @@ export default {
this.$emit('update:value', value);
},

updateDebounced: debounce(function (value) {
this.update(value);
}, 150),

updateMeta(value) {
this.$emit('update:meta', value);
},
},

created() {
this.updateDebounced = markRaw(debounce((value) => {
this.update(value);
}, 150));
},

computed: {
publishContainer() {
// The injectedPublishContainer contains refs. We'll unwrap everything so that we can do
Expand Down
17 changes: 10 additions & 7 deletions resources/js/components/fieldtypes/bard/BardFieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,20 @@ export default {

if (JSON.stringify(json) === JSON.stringify(oldJson)) return;

// Temporarily disable debouncing.
this.debounceNextUpdate = false;

this.debounceNextUpdate
? this.updateDebounced(json)
: this.update(json);

const shouldDebounce = this.debounceNextUpdate;
this.debounceNextUpdate = true;

if (shouldDebounce) {
this.updateDebounced(json);
} else {
this.updateDebounced.cancel();
this.update(json);
}
},

value(value, oldValue) {
if (!this.editor) return;

const oldContent = this.editor.getJSON();
const content = this.valueToContent(value);

Expand Down
9 changes: 8 additions & 1 deletion resources/js/util/debounce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function (func, wait, immediate) {
let timeout;

return function () {
const debounced = function () {
const context = this,
args = arguments;

Expand All @@ -14,4 +14,11 @@ export default function (func, wait, immediate) {
if (!immediate) func.apply(context, args);
}, wait);
};

debounced.cancel = function () {
clearTimeout(timeout);
timeout = null;
};

return debounced;
Comment on lines +17 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's blowing my mind a bit that debounced can be a function but can also have properties? wha?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, there are also some build in ones like debounced.name, debounced.length .. functions are objects :D

}