How to make a visual editor in JavaScript. How to display Word document (.doc.,docx) in browser using JavaScript

You must have come across many visual editors, allowing you to quite conveniently form the appearance of pages, or some messages, for example, on a forum. And not with BB codes, and immediately get a specific result. There are a lot of these editors on the Internet. One of the most popular is TinyMCE, however, I am a supporter of my own scripts and believe that For every problem there must be a solution., not universal. Therefore, in this article, I will how to create visual editor in JavaScript.

Here is some well-commented code:







At first glance, the code seems to be very complex. And so it is, but if we consider it by the basic elements, then there is nothing complicated. There is regular form and field hidden, where the resulting in the editor is saved HTML code. The editor itself is a regular frame, that is, a regular HTML page, in which we can write text ( designMode="on"). And various formattings are created using the method execCommand() A that executes the command specified in the parameter. And I think it will not be difficult for you to create new buttons and attach similar handlers to them.

There is nothing superfluous in this script, only the most important and basic, and it can be taken as a basis for creating your own visual editor in JavaScript, in which there will be nothing superfluous, but only what you need.

Can anyone suggest me any library in javascript document editor(client end) where user can view and edit documents like doc, docx, odf etc.

1 Reply


1

Your best bet is to look for converters and then edit the converted content.

I've been looking for a working solution for a couple of weeks now and the best I've found so far is docx2html .

Depending on the development platform, you can also use Google "ONLYOFFICE Document Server Community Edition They have in page editors for Word, Excel and Powerpoint - but they are asp. It's an open source platform and available on Github. Might be worth a look though.


Is it possible to convert doc/docx to html using jquery/javascript?

I need to convert doc/docx to html. I have files stored in Amazon s3. I need to view & edit it in the browser. Right now, I'm using Apache POI to...


Generating docx documents from javascript docx template

I'm looking for a library in javascript that can generate docx documents from a docx template and can replace tags with their values ​​and replace images with other images I found the name of the library...


how to judge if file is doc or docx in POI

The name can be a little confusing. The simplest method should be based on the extension name just like: // is represents the InputStream if (filePath.endsWith(doc)) ( WordExtractor ex = new...


doc or docx: is there a safeway for type identification from "requests" in python3?

1) How can I distinguish doc and docx files from requests? a) for example if I have url="https://www.iadb.org/Document.cfm?id=36943997" r = requests.get(url,timeout=15)...


Import doc and docx files in .Net and C#

I write text editor and want to add the ability to import .doc and .docx files. I know I can use OLE Automation but if I use a recent OLE library it won't...


Document editing (DOC, DOCX, RTF, TXT) in Webforms

I am developing a web application using asp.net 3.5. This app has many documents like .doc, .docx, .rtf, .pdf, .txt, etc... and sometimes usar needs to edit those documents. V...


Convert DOC/DOCX to Semantic HTML

I would like to convert doc/docx documents to semantic HTML. Some wishes/requirements: Semantic HTML such that the headings in the document are

,

etc., tables -...


Golang packages to convert doc to docx and docx to pdf?

I am writing a web application in Golang where the user can upload a doc or docx file. Part of the content of the file will be changed according to the predefined format. Later the user...


Load Rich Word Document into RichText Editor telerik editor

I have some form where I upload some word document and save it to DB. Later I have to do some marking of these documents. Currently I have not found anything for...


Convert doc file to docx format

I have converted the docx file to html as it is in Zip format. but i need to convert doc files too. Is there any way to change doc file to docx using code(prefer java). Please...

I have successfully made the code to display a PDF file in the browser instead of an "open/save" dialog. Now I'm stuck trying to display a word document in the browser. I want to display a Word document in Firefox, IE7+, Chrome, etc.

can anyone help? I always get an "open/save" dialog when displaying the word doc in the browser. I want to implement this functionality with JavaScript.

6 responses

however if you prefer to have native support, in most if not all browsers I would recommend resaving .doc / .docx as a PDF they can also be independently rendered with Mozilla's PDF.js.

Brandon's and fatbotdesigns answers are both correct, but by implementing preview Google documents, we found some .docx files that cannot be processed by Google. Switched to MS Office online preview and it works like a charm.

https://view.officeapps.live.com/op/embed.aspx?src=http://remote.url.tld/path/to/document.doc"

there seems to be some js libraries that can handle .docx (no .doc) for client side html conversion (in no particular order):

    https://github.com/lalalic/docx2html - docx to html, most elements are supported

    https://github.com/mwilliamson/mammoth.js - supports headers, lists, tables, footnotes, footnotes, images and box text!--1-->

    https://www.npmjs.com/package/docx2html - converting HTML documents to DOCX in the browser

    https://github.com/artburkart/docx2html - apparently works in the browser

Note: If you are looking for The best way convert doc/docx file on client side, then probably the answer is no need. If you really need to do it, do it. on the server side i.e. with libreoffice in headless mode, apache-poi or any other library is best for you.

ViewerJS is useful for viewing/embedding openoffice format like odt,odp,ods as well as pdf.

to embed openoffice / pdf document

/ViewerJS/ is the ViewerJS path

#../demo/ohm2013 - the path of your file you want to add

This will remove any runtime dependencies from Google services and Microsoft (eg if they were, or you were limited by them).

It also has the advantage that you can expand to other file types if you like (PPTX, XLS, DOC, etc.)

The code editor is a handy tool for every programmer. And everyone chooses for themselves: someone appreciates functionality, someone - mobility, for someone the main thing is design and convenience. Some even like to write code in Notepad, but it's like trying to build a house with a hammer.

JavaScript is a powerful and capricious language. On the one hand, a lot of frameworks and libraries, on the other hand, not the easiest syntax and the dangers associated with "dynamics". Therefore, to work with it, it is important to choose an editor. Right choice will keep your code clean, high speed development, a minimum of errors and pleasure from work. Choosing one of the hundreds of existing editors will take a lot of time, so we have done some of the work for you. Here are 5 of the best.

webstorm

WebStorm from JetBrains is great in both its manifestations: as an IDE it supports working with version control systems, allows you to remotely deploy code, as an editor - standard amenities, such as syntax highlighting, autocompletion, navigation.

Advantages:

  • LiveEdit - view the changes made to the code without the need to save it;
  • interaction with frameworks, such as React, Angular, Meteor;
  • more than a hundred built-in tests for error detection;
  • integration with Mocha, Protractor, Jest, Karma for unit tests;
  • a full-scale debugger for debugging code on the server and client sides;
  • navigation for simultaneous work with several files;
  • code completion, syntax highlighting.

Flaws:

  • costs $129 for the first year of operation;
  • for novice coders, the functionality is redundant.

Visual Studio Code

An offshoot of the Visual Studio IDE focused on working with code. It is easy to learn, easy to use, and yet functional.

Advantages:

  • contextual completion of both syntax and used variables, modules, functions, etc.;
  • debugger with breakpoints, call stack, interactive console;
  • support for snippets and templates;
  • integration with Git;
  • convenient and simple interface;
  • free editor.

Flaws:

  • few plugins.

Sublime Text

A convenient and time-tested cross-platform editor with a customizable interface and the ability to perform trivial actions using hotkeys.

Advantages:

  • Hotkeys;
  • code navigation in the form of a mini-map;
  • the ability to change the visual theme;
  • snippet support;
  • highlighting, auto-completion of variables and syntax;
  • multiple editing through the use of pointers;
  • support for build systems;
  • syntax check right at the time of input;
  • autosave.

Flaws:

  • full version costs $70;
  • lack of a code analyzer for placing links.

atom editor

Introduced in 2015, a code editor from Git that copies the design of Sublime Text and wrapped in Chromium.

Advantages:

  • more than 50 open modules;
  • convenient and pleasant interface;
  • free;
  • code completion and highlighting;
  • package manager, of which there are already more than 3.5 thousand;
  • flexible settings of the editor, plug-ins, interface themes;
  • editing and navigation using hotkeys.

Flaws:

  • low performance;
  • empty package "out of the box".

Brackets

In 2014, Brackets scared programmers away with bugs and shortcomings, but now it is gradually returning trust with new high-quality functionality.

Advantages:

  • rich equipment "out of the box";
  • Live Preview mode - real-time preview of edits in the browser;
  • package manager;
  • showing in the code used images and colors;
  • autocompletion and syntax highlighting;
  • code analyzer;
  • free.

Flaws:

  • strong focus on the web and HTML+CSS+JavaScript;
  • slow development;
  • slow performance due to preview functions.

Write JavaScript code you can also use the regular Notepad - just save the result with the .js extension, and it will not only connect to the site, but even work. However, using such a limited editor will greatly slow down development. Therefore, it is better to take something specialized. There are several main criteria by which you need to choose an editor for working with JavaScript code.

First, syntax highlighting is needed: names, operators and special characters should be displayed differently so that you can quickly read the code.

Syntax highlighting in Notepad++

Secondly, you need auto-completion: when you enter the name of a function or variable, the editor should suggest several options for commands that you probably want to enter. It is desirable that it also show hints for the selected component - for example, required arguments, variable type, short description etc.

Autocomplete in Notepad++

Thirdly, we need the ability to hide individual blocks. When working on large projects, this is useful because you don’t have to scroll through huge pieces of code to find the right one.

Hiding blocks in Visual Studio Code

It is desirable that errors are highlighted - then you can immediately notice and correct them:

Displaying Errors in Visual Studio Code

Good bonuses will be the ability to work with several files in one window, a project manager, choosing themes, installing plugins, and so on.

Exists a large number of editors that meet these requirements and are suitable for working with JavaScript. What to write code on is a personal matter for everyone, but many people have problems choosing the right program, so we have compiled a list of 6 good tools.

Notepad++

Notepad++ is a simple and user-friendly open source editor. It has syntax highlighting for several languages, including JS, automatic formatting, and auto-completion. There is navigation in the form of tabs, file manager and code cards.

Working with different encodings, connecting compilers, using plugins and others are available useful features. For example, adding QuickText will improve the built-in autocompletion.

Notepad++ is updated regularly, so bugs are quickly fixed and new features added. Unfortunately, it only works on Windows.

vim

The Vim editor has long been a classic. Many people say that it’s hard to figure it out, but if you give it enough time, you won’t want to use something else.

In addition to meeting all the criteria we've chosen, Vim is also fully customizable, so you can:

  • install plugins;
  • change topics;
  • assign hotkeys and so on.

Vim is completely free and runs on all platforms from Windows, MacOS and Linux to MS DOS and OS/2.

Microsoft Visual Studio

Microsoft Visual Studio is an IDE that has everything you need to work with JS. In addition to the usual syntax highlighting and auto-completion, you can also use the debugger and create Unit tests.

However, the program weighs quite a lot and loads the system heavily, so it is not very suitable for working on weak computers.

A monthly subscription can cost anywhere from $45 to $250 per month, but there are also free version for beginner developers and those who create open source programs. The IDE is available for Windows and MacOS, and on Linux it can be installed using Wine.

Visual Studio Code

Visual Studio Code is a lightweight (~170 MB vs 30 GB), cross-platform and free version of Visual Studio. This editor comes with support for JavaScript, TypeScript, and Node.JS.

Extensions for other languages ​​can be downloaded directly from Visual Studio Code.

Built-in debugger and Git commands for working with the GitHub version control system allow you to speed up testing and publishing applications.

Sublime Text

Sublime Text is a simple cross-platform editor. Its interface is customizable, and you can perform some actions using hot keys.

It also has support for snippets, auto-completion, syntax highlighting, and navigation. Cool bonus - autosave. However, it is more needed for beginners - after all, professional programmers over the years develop the habit of saving after entering each command.

The main disadvantage of Sublime Text is that the full version costs $ 80 (the license is issued for one user who can use the editor on any computer). Otherwise it fits well
JS developers.