Reputation: 153
We have different templates built in angular 8 which contain sliders, product listing, and some blocks with images, text etc. I need to create a drag n drop page builder, where user can select a template, add or remove any element and edit elements (styles) e.g product box with image, title, price, button etc. I don't know how to proceed with technology stack which will finally save templates to be served into angular. mostly all components show dynamic data loaded from API
Any help will be much much appreciated and I'm open for any discussion
Upvotes: 0
Views: 2299
Reputation: 1126
From your description it sounds like you are trying to build a dashboard system or HTML WYSIWYG editor. this is a fairly broad architectural question, and the approaches will very depending on your project's requirements/timeframe etc... My first recommendation would be to do a search to see if there are any tools out there that already do what you want. i.e. does Ck Editor with customization give you want you need? ...
failing to find one, if you need to roll your own, here are a few lessons learned from my experience with this.
Define what your page model will look like
export interface Widget {
name: string;
type: string;
style: CSSProperties;
widgets?: Widget[];
}
export interface Text extends Widget {
textContent: string;
}
export interface Image extends Widget {
url: string;
}
export interface Table extends Widget {
source: string;
}
you can always augment your interfaces, but at least spend sufficient time understanding what structure will look like. if done property, adding new components with their own interfaces becomes a breeze. in some ways, this is where using HTML as the model may make more sense.
but with this model in place, you "TextComponent" can expect a data to be provided to it that conforms to the "Text" interface. same for image, table etc...
with this you can tag your component's html with the specific property you wish to use. i.e. in the case of a TextComponent, you would simply use "{{textContent}}
in your HTML since it is a property of the interface for this Component.
Drag and Drop
whichever approach you take, all drag and drop have a 'drop' event. this event is triggered when the user stops dragging. most drag and drops tools also allow you to pass data during the 'drag' even so that the 'drop' event handler can process it.
Once you have defined your models, you could pass the model for the current component in the drag event. and on "Drop" you can check the widget.type
of the data passed. this will allow you to determine what type of component you are dealing with.
Make use of Observables to ensure individual components remain isolated from each other
we ended up going with a state management solution (NgRX), to help separate state from layout. Since we did it midway through, it was a pain initially, but once done, adding new components became much faster. FYI: You don't need to use NgRx, a service or shared observable that will keep track of your various component's data will do just as well. Using RxJS BehaviorSubject to allow late subscribers to get the current value for example
this allowed for handling scenarios such as
ultimately this approach allowed us to decouple the presentation logic of our components from the data logic.
this can get very complicated depending on how complex you need it to be. if this is a project that you simply need to get something up quick and dirty, then of course do what you must. however, if you have the time to dedicate to this, I would suggest spending some quality time with understanding your model as well as what the component interaction will be.
I hope some of this was helpful. best of luck.
Upvotes: 2