Thursday, December 05, 2013

Using Wyzz Web-based HTML editing control in ASP.NET MVC

I recently had to put out a web-based single page application (SPA) on short notice. To make that happen, I knew I had to use some open-source controls. One was the jsTree treeview control (which I wrote about on this blog - Using jsTree with ASP.NET MVC) and another was the Wyzz WYSIWYG web-based editing control for HTML.

From their site, “Wyzz is an ultra-small, very light WYSIWYG (What You See Is What You Get) Editor for use in your web applications. It's written in JavaScript, and is free (as in speech and as in beer) for you to use in your web applications and/or alter to your needs (see the license conditions).

image

Naturally, the first step to add a reference to the wyzz.js script file. Once you have that, you just need to add the control to an HTML <textarea> element. Finally, it’s a simple matter of adding some JavaScript to “make_wyzz” the control.

<script language="JavaScript" type="text/javascript" src="~/Home/wyzz.js"></script>



<textarea name="textEditor" id="textEditor" rows="10" cols="40">No file loaded...</textarea><br />
<script language="javascript1.2">
    make_wyzz('textEditor');
</script>
<div ng-controller="EditorCtrl">
<form novalidate class="simple-form">
<button ng-click="saveFileContent()">save</button>
</form>
</div>

As you can see in the example above, I’ve chosen to use an AngularJS control to define the behaviour of the save button. In the JavaScript I define a server-side controller function (ASP.NET in this case) and I send it the content of the control by accessing the HTML element that the control is using.

$scope.saveFileContent = function () { 
$http.post('/Home/SaveFileContent', { filePath: document.getElementById("multilingualfile").innerHTML, content: document.getElementById("wysiwyg" + "textEditor").contentWindow.document.body.innerHTML, title: document.getElementById("titleHtml").value })
.then(
function (response) {
alert("File Save Result: " + response.data.Result);
},
function (data) {
alert("Error saving file content");
}
);
}

Update: Here’s the basic format of the server-side part:


[HttpPost]
public ActionResult SaveFileContent(string filePath, string content, string title)
{
try
{
...

return Json
(
new
{
Result = "Success",
}
);
}
catch (Exception ex)
{
...

return Json
(
new
{
Result = "Error saving content: " + ex.ToString(),
}
);
}
}

To customize your Wyzz controls, you can edit the wyzz.js file. If you have any issues, refer to the Wyzz discussion forum.

2 comments:

Phil Ballard said...

Hi Stephen
I'm the author of Wyzz - thanks for sharing this information for others to find it.
Just one quick word: the forum on the site is currently closed to new registrations due to a particularly bothersome spambot; while I sort it out, please message me via the site if you need a forum registration, and I'll set you one up.
Many thanks!

Stephen Cawood said...

Thanks Phil, and thanks for being so responsive.