Skip to content

Commit

Permalink
Fix editonclick bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Oct 1, 2024
1 parent 8e93f2a commit b84556f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/powergrid.js": "/powergrid.js?id=377a2e5641b0803a4bcc836ddc46270d",
"/powergrid.js": "/powergrid.js?id=df5ea910257b1759edb78c977a3ae6c8",
"/bootstrap5.css": "/bootstrap5.css?id=a27af22343149104b2aa3283d8fd502b",
"/tailwind.css": "/tailwind.css?id=924477e2afcb2cb56aa392e266ee56ca"
}
2 changes: 1 addition & 1 deletion dist/powergrid.js

Large diffs are not rendered by default.

37 changes: 23 additions & 14 deletions resources/js/components/pg-editable.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
export default (params) => ({
theme: params.theme,
editable: false,
tableName: params.tableName ?? null,
id: params.id ?? null,
dataField: params.dataField ?? null,
content: params.content,
oldContent: null,
fallback: params.fallback,
inputClass: params.inputClass,
saveOnMouseOut: params.saveOnMouseOut,
oldContent: null,
editable: false,
hash: null,
hashError: true,
showEditable: false,
editableInput: '',
inputClass: params.inputClass,
saveOnMouseOut: params.saveOnMouseOut,
init() {
if (this.content.length === 0 && this.fallback) {
this.content = this.htmlSpecialChars(this.fallback);
Expand All @@ -28,8 +27,16 @@ export default (params) => ({
this.oldContent = this.content;
const editablePending = window.editablePending.notContains(this.hash)
this.hashError = editablePending

setTimeout(() => {
if (window.editablePending.getTextContent(this.hash) && document.getElementById('editable-' + this.hash)) {
document.getElementById('editable-' + this.hash).textContent =
window.editablePending.getTextContent(this.hash)
}
}, 220)

if (editablePending) {
const pendingHash = window.editablePending.pending[0]
const pendingHash = window.editablePending.get(this.hash)
document.getElementById('clickable-' + pendingHash).click()
} else {
showEditable = true
Expand Down Expand Up @@ -60,12 +67,13 @@ export default (params) => ({
this.content = this.htmlSpecialChars(this.content);
},
save() {
if(this.$el.textContent == this.oldContent) {
this.editable = false;
this.showEditable = false;
window.editablePending.clear()
window.editablePending.set(this.hash, this.$el.textContent)

return;
}
setTimeout(() => {
document.getElementById('clickable-' + this.hash).textContent =
this.$el.textContent
}, 230)

setTimeout(() => {
window.addEventListener('pg:editable-close-'+this.id, () => {
Expand All @@ -75,16 +83,17 @@ export default (params) => ({
})

if(!window.editablePending.has(this.hash)) {
window.editablePending.set(this.hash)
window.editablePending.set(this.hash, this.$el.textContent)

}

this.$wire.dispatch('pg:editable-' + this.tableName, {
this.$wire.dispatch('pg:editable-' + this.$wire.tableName, {
id: this.id,
value: this.$el.textContent,
field: this.dataField
})

this.oldContent = null
this.oldContent = window.editablePending.getTextContent(this.hash)

this.$nextTick(() => setTimeout(() => {
this.focus()
Expand Down
28 changes: 22 additions & 6 deletions resources/js/stores/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
document.addEventListener('alpine:init', () => {
window.Alpine.store('editablePending', {
pending: [],
set(value) {
this.pending.push(value)

set(value, textContent) {
this.pending = this.pending.filter(item => item.value !== value);
this.pending.push({ value, textContent });
},

has(value) {
return this.pending.includes(value)
return this.pending.some(item => item.value === value);
},

get(value) {
return this.pending.find(item => item.value === value);
},

notContains(value) {
return this.pending.length > 0 && !this.pending.includes(value)
return this.pending.length > 0 && !this.has(value);
},

clear() {
this.pending = []
this.pending = [];
},

isNotEmpty() {
return this.pending.length > 0
return this.pending.length > 0;
},

getTextContent(value) {
const item = this.pending.find(item => item.value === value);
return item ? item.textContent : null;
}
});


window.Alpine.store('pgBulkActions', {
selected: [],
init() {
Expand Down

0 comments on commit b84556f

Please sign in to comment.