-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Pages datasources #6601
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
Merged
Merged
Pages datasources #6601
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
cc7b2de
Add tests for component wrapper
mohamedsalem401 e7aede4
Refactor component data collection
mohamedsalem401 d81686d
Add data resolver to wrapper component
mohamedsalem401 0cb1a83
Fix types
mohamedsalem401 af2c99f
Add collection data source to page
mohamedsalem401 efd75e3
Merge branch 'dev' of https://github.com/GrapesJS/grapesjs into pages…
mohamedsalem401 436c064
refactor get and set DataResolver to componentWrapper
mohamedsalem401 59096ea
Rename key to __rootData
mohamedsalem401 7df0c9f
add resolverCurrentItem
mohamedsalem401 4ba97dd
Make _resolverCurrentItem private
mohamedsalem401 1bf25c0
update ComponentWrapper tests
mohamedsalem401 b6181ad
Fix componentWithCollectionsState
mohamedsalem401 608fdd9
remove collectionsStateMap from Page
mohamedsalem401 9e68c92
update component wrapper tests
mohamedsalem401 edd9516
fix component wrapper tests
mohamedsalem401 da8daa8
return a copy of records for DataSource.getPath
mohamedsalem401 094b0da
Move all collection listeners to component with collection state
mohamedsalem401 b96dee5
fix style sync in collection items
mohamedsalem401 2f8c198
fix loop issue
mohamedsalem401 7772c7d
update data collection tests
mohamedsalem401 eb9b4ff
cleanup
mohamedsalem401 4499b06
update collection statemap on wrapper change
mohamedsalem401 6a71c87
Add object test data for wrapper data resolver
mohamedsalem401 fb84c3c
cleanup
mohamedsalem401 b82eb8c
up unit test
mohamedsalem401 aef8269
remove duplicated code
mohamedsalem401 f26a365
cleanup event path
mohamedsalem401 d2c2579
update test data to better names
mohamedsalem401 56f46fd
improve component data collection performance
mohamedsalem401 584fe1d
cleanup tests and types
mohamedsalem401 b600eb6
Merge branch 'dev' of https://github.com/GrapesJS/grapesjs into pages…
mohamedsalem401 7d80b00
fix performance issue for the new wrapper datasource
mohamedsalem401 470c451
Undo updating component with datacolection tests
mohamedsalem401 ac43e1e
apply comments
mohamedsalem401 4704d05
Skip same path update
artf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/core/src/data_sources/model/ComponentWithCollectionsState.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { DataCollectionStateMap } from '../../data_sources/model/data_collection/types'; | ||
import DataResolverListener from '../../data_sources/model/DataResolverListener'; | ||
import DataVariable, { DataVariableProps, DataVariableType } from '../../data_sources/model/DataVariable'; | ||
import Components from '../../dom_components/model/Components'; | ||
import Component from '../../dom_components/model/Component'; | ||
import { ObjectAny } from '../../common'; | ||
import DataSource from './DataSource'; | ||
import { isArray } from 'underscore'; | ||
|
||
export type DataVariableMap = Record<string, DataVariableProps>; | ||
|
||
export type DataSourceRecords = DataVariableProps[] | DataVariableMap; | ||
|
||
export default class ComponentWithCollectionsState<DataResolverType> extends Component { | ||
collectionsStateMap: DataCollectionStateMap = {}; | ||
dataSourceWatcher?: DataResolverListener; | ||
|
||
constructor(props: any, opt: any) { | ||
super(props, opt); | ||
this.listenToPropsChange(); | ||
} | ||
|
||
onCollectionsStateMapUpdate(collectionsStateMap: DataCollectionStateMap) { | ||
this.collectionsStateMap = collectionsStateMap; | ||
this.dataResolverWatchers?.onCollectionsStateMapUpdate?.(); | ||
|
||
this.components().forEach((cmp) => { | ||
cmp.onCollectionsStateMapUpdate?.(collectionsStateMap); | ||
}); | ||
} | ||
|
||
syncOnComponentChange(model: Component, collection: Components, opts: any) { | ||
const prev = this.collectionsStateMap; | ||
this.collectionsStateMap = {}; | ||
super.syncOnComponentChange(model, collection, opts); | ||
this.collectionsStateMap = prev; | ||
this.onCollectionsStateMapUpdate(prev); | ||
} | ||
|
||
setDataResolver(dataResolver: DataResolverType | undefined) { | ||
return this.set('dataResolver', dataResolver); | ||
} | ||
|
||
get dataResolverProps(): DataResolverType | undefined { | ||
return this.get('dataResolver'); | ||
} | ||
|
||
protected listenToDataSource() { | ||
const path = this.dataResolverPath; | ||
if (!path) return; | ||
|
||
const { em, collectionsStateMap } = this; | ||
this.dataSourceWatcher?.destroy(); | ||
this.dataSourceWatcher = new DataResolverListener({ | ||
em, | ||
resolver: new DataVariable({ type: DataVariableType, path }, { em, collectionsStateMap }), | ||
onUpdate: () => this.onDataSourceChange(), | ||
}); | ||
} | ||
|
||
protected listenToPropsChange() { | ||
this.on(`change:dataResolver`, () => { | ||
this.listenToDataSource(); | ||
}); | ||
|
||
this.listenToDataSource(); | ||
} | ||
|
||
protected get dataSourceProps(): DataVariableProps | undefined { | ||
return this.get('dataResolver'); | ||
} | ||
|
||
protected get dataResolverPath(): string | undefined { | ||
return this.dataSourceProps?.path; | ||
} | ||
|
||
protected onDataSourceChange() { | ||
this.onCollectionsStateMapUpdate(this.collectionsStateMap); | ||
} | ||
|
||
protected getDataSourceItems(): DataSourceRecords { | ||
const dataSourceProps = this.dataSourceProps; | ||
if (!dataSourceProps) return []; | ||
const items = this.listDataSourceItems(dataSourceProps); | ||
if (items && isArray(items)) { | ||
return items; | ||
} | ||
|
||
const clone = { ...items }; | ||
return clone; | ||
} | ||
|
||
protected listDataSourceItems(dataSource: DataSource | DataVariableProps): DataSourceRecords { | ||
const path = dataSource instanceof DataSource ? dataSource.get('id')! : dataSource.path; | ||
if (!path) return []; | ||
let value = this.em.DataSources.getValue(path, []); | ||
|
||
const isDatasourceId = path.split('.').length === 1; | ||
if (isDatasourceId) { | ||
value = Object.entries(value).map(([_, value]) => value); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
protected getItemKey(items: DataVariableProps[] | { [x: string]: DataVariableProps }, index: number) { | ||
return isArray(items) ? index : Object.keys(items)[index]; | ||
} | ||
|
||
private removePropsListeners() { | ||
this.off(`change:dataResolver`); | ||
this.dataSourceWatcher?.destroy(); | ||
this.dataSourceWatcher = undefined; | ||
} | ||
|
||
destroy(options?: ObjectAny): false | JQueryXHR { | ||
this.removePropsListeners(); | ||
return super.destroy(options); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.