Overview

Chapter 3. File editing and management: rich formatting, file storage, drag and drop

This chapter presents HTML5 as a platform for building desktop-like web applications, using a client-side HTML editor as the main example. The sample app combines a visual rich-text editor with a raw markup editor, lets users create and organize files locally, and adds location-aware content so the editor can insert a map based on the user’s current position.

At a high level, the chapter shows how to structure the application into browser and editor views, manage navigation, and keep editing state consistent as users move between modes. It explains how editable documents work with designMode and how execCommand can power formatting controls such as bold, italic, links, lists, and images. It also introduces geolocation as a practical enhancement, showing how a user’s coordinates can be requested and turned into a map inserted directly into the document.

The chapter then focuses on local file management by using HTML5 file-related APIs to create a sandboxed filesystem, request storage quota, list files, load file contents, and support viewing, editing, deleting, creating, importing, saving, and previewing HTML documents. Finally, it extends the app with drag-and-drop so users can bring files into the application and export them back out again, tying together rich editing, local storage, and intuitive file transfer into a complete browser-based editing workflow.

The two views of the Super HTML5 Editor application are shown. The File Browser view (left) allows users to manipulate the files stored in the app; the File Editor view (right) enables the file to be modified using rich-text editing controls or directly using HTML markup.
Listing 3.1. index.html—Application HTML structure
Listing 3.2. index.html–File Browser view markup
Listing 3.3. index.html–File Editor view markup
Listing 3.4. app.js–Application initialization code
Listing 3.5. app.js—View navigation and state management code
When you load the application right now, a hash value #list will be appended to the end of the URL. To navigate to the editor view manually, change this to #editor/index.html as shown.
Listing 3.6. app.js—Enabling designMode and connecting the two editors
The isDirty variable we created earlier allows the application to keep track of whether the user has made changes to the document. If they’ve made changes and try to close the window without saving, they’ll be shown this warning message to confirm they want to leave the page.

3.2.2. Providing rich-text editing controls with execCommand

Listing 3.7. app.js–Implementing the rich-text editing toolbar in the visual editor
Try it out—and challenge yourself!

3.2.3. Mapping a user’s current location with the Geolocation API

Listing 3.8. app.js–Using geolocation to insert a map of the user’s location
The browser will request the user’s permission to enable the Geolocation API. If access is denied, the browser will behave as though it doesn’t support geolocation.
A map of the user’s location will be added to the editor. This map is actually an image generated by the Google Maps Static API. Easy, huh?
Step 1: Create a persistent filesystem
Listing 3.9. app.js–Creating a persistent filesystem
Listing 3.10. app.js—Building the file list UI from an array of files
Listing 3.11. app.js—Reading the file list using the directory reader
Step 3: Load files in the File Editor view using the File API
Listing 3.12. app.js—Loading files in the File Editor view
Step 4: View, edit, and delete files in the filesystem
Listing 3.13. app.js—Viewing, editing, and deleting files
Listing 3.14. aapp.js—Creating a new empty file
The file index.html has been successfully created!
Step 6: Import existing files from the user’s computer
Listing 3.15. app.js—Importing files from the user’s computer
After the user clicks the Choose Files button, a dialog window pops up.
Listing 3.16. app.js—Saving files using the File Writer API

3.4.1. Dragging files into an application for import

Listing 3.17. app.js—Allowing users to import files by dropping them in the application

3.4.2. Dragging files out of an application for export

Listing 3.18. app.js—Allowing users to export files by dragging them out of the app

FAQ

What is the main goal of Chapter 3 in HTML5 in Action?The chapter shows how to build a client-side HTML editor called the Super HTML5 Editor, using HTML5 APIs for rich-text editing, local file storage, geolocation, and drag-and-drop file import/export.
What are the two main views in the Super HTML5 Editor application?The application is split into a File Browser view and a File Editor view. The File Browser lets users create, import, view, edit, delete, and export files, while the File Editor lets users modify content using visual editing or raw HTML markup.
Why does the chapter use the
and elements in the File Browser view?They are used to create collapsible sections in the interface without relying on JavaScript and CSS tricks. The element provides the clickable label for the expanded
content.
What does the designMode property do in rich-text editing?Setting designMode to on makes an entire document editable, which is useful for turning an iframe into a WYSIWYG editor. It allows the user to edit the full contents of the document directly.
When should contenteditable be used instead of designMode?Use contenteditable when you want only a specific element to be editable rather than the whole document. It is widely supported and is better for editing a limited region of a page.
What is execCommand used for in the chapter?execCommand applies editing and formatting actions to the current selection or caret position in an editable document. It is used for toolbar actions such as bold, italic, linking, inserting images, and other rich-text commands.
How does the application insert a map of the user’s location?The app uses the Geolocation API to get the user’s coordinates with getCurrentPosition(), then uses Google Maps to generate a static map image. That image is inserted into the editor using execCommand with insertImage.
What is the purpose of the File System API in this chapter?The File System API provides a sandboxed local storage area where the application can create, load, edit, save, and delete files on the client side. The chapter uses a persistent filesystem so the user’s work can be stored for later use.
How do the File API and File Writer API work together in the application?The File API is used to read file contents as text, while the File Writer API is used to write data back into files. Together, they let the app load imported files and save edited content.
How does drag and drop improve file import and export in the application?Drag and drop lets users import files by dropping them into the app and export files by dragging them out. The chapter uses the drop and dragover events for importing, and the draggable attribute plus dragstart for exporting.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • HTML5 in Action HTML5 in Action ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • HTML5 in Action HTML5 in Action ebook for free