3

Reuse Angular Document Editor Instances in Bootstrap Tabs

 1 year ago
source link: https://www.textcontrol.com/blog/2023/05/22/reuse-angular-document-editor-instances-in-bootstrap-tabs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Reuse Angular Document Editor Instances in Bootstrap Tabs

The TX Text Control document editor available Angular, is initialized as a singleton instance on an HTML page. Similar to routes in Angular, the editor typically needs to be removed from the DOM and recreated at the new location. However, some layouts require a fast initialization process and fluid switching between instances, such as tab views:

TX Text Control Angular in Bootstrap Tabs

Bootstrap Tab Pages

For these cases, the fully created editor can be moved within the DOM to a new active tab. Consider the following bootstrap layout:

The editor is encapsulated in an additional DIV element with the id tx-editor. When the active tab is switched, this complete element is moved to the new active tab. Additionally, the content of the current instance is saved to local storage and restored when switching back. The following JavaScript (jQuery) code attaches two events to the tab list view:

  • hide.bs.tab: Occurs when the previous tab is about to be hidden.
  • show.bs.tab: Occurs when the newly active tab is about to be shown.

Saving the Document

For the previous tab, the document is saved using the saveDocument method and stored in a local storage variable with the associated tab name (aria-controls).

Loading the Document

For the current tab, the local storage is checked for a previously saved document. If a document is found, it is loaded into the document editor using the loadDocument method. Then the editor element is moved to the new tab position in the DOM using the appendTo method:

If no document is saved for the currently active tab, the contents are reset and the item is moved:


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK