I am using TinyMce from within a Windows Form through the Awesomium browser. I created a User Control like this that I am placing on the Windows Form:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports Awesomium.Core
Imports Awesomium.Windows
Partial Public Class TinyMCE
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Public Property HtmlContent() As String
Get
Dim content As String = String.Empty
If Not WebCore.IsInitialized Then
WebCore.Initialize(New WebConfig() With {.LogLevel = LogLevel.Verbose})
Else
Dim html As JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent")
content = html.ToString
End If
Return content
End Get
Set(value As String)
If WebCore.IsInitialized Then
Dim objResult As JSObject = webBrowserControl.ExecuteJavascriptWithResult("window")
objResult.InvokeAsync("SetContent", value)
End If
End Set
End Property
Public Sub CreateEditor()
If File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce\js\tinymce\tinymce.min.js")) Then
webBrowserControl.Source = New Uri("file:///" & Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce.htm").Replace("\"c, "/"c))
Else
MessageBox.Show("Could not find the tinyMCE script directory. " & (Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce\js\tinymce\tinymce.min.js")), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
End Sub
End Class
My HTML is fairly simplistic as far as the TinyMCE goes:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>tinyMCE WYSIWYG Editor</title>
</head>
<script type="text/javascript" src="tinymce/js/tinymce/tinymce.min.js"></script>
<!-- Script functions to expose tinyMCE internals that get called from code using InvokeScript method. -->
<script type="text/javascript">
function GetContent() {
return tinyMCE.get('tinyMceEditor').getContent();
}
function SetContent(htmlContent) {
tinyMCE.get('tinyMceEditor').setContent(htmlContent);
}
</script>
<!-- TinyMCE -->
<script type="text/javascript">
tinymce.init({
selector: 'textarea',
menu: {
file: { title: 'File', items: 'newdocument' },
edit: { title: 'Edit', items: 'undo redo | cut copy paste pastetext | selectall' },
insert: { title: 'Insert', items: 'link media | template hr' },
view: { title: 'View', items: 'visualaid' },
format: { title: 'Format', items: 'bold italic underline strikethrough superscript subscript | formats | removeformat' },
table: { title: 'Table', items: 'inserttable tableprops deletetable | cell row column' },
tools: { title: 'Tools', items: 'spellchecker code' },
plantwatch: { title: 'My Menu', items: 'dataloop collectorloop processloop historian emailgroup alertgroup menusave' }
},
menubar : 'plantwatch file edit insert view format table tools',
plugins: [
'advlist autolink lists link image charmap print preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking save table contextmenu directionality',
'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc'
],
toolbar1: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
toolbar2: 'forecolor backcolor emoticons | codesample',
image_advtab: true,
setup: function (editor) {
editor.addMenuItem('dataloop', {
text: 'Data Loop',
onclick: function () {
editor.insertContent('{LOOP:Data}');
}
});
editor.addMenuItem('collectorloop', {
text: 'Collector Loop',
onclick: function () {
editor.insertContent('{LOOP:Collector}');
}
});
editor.addMenuItem('processloop', {
text: 'Process Loop',
onclick: function () {
editor.insertContent('{LOOP:Process}');
}
});
editor.addMenuItem('historian', {
text: 'Historian Server Name',
onclick: function () {
editor.insertContent('{HistorianServerName}');
}
});
editor.addMenuItem('emailgroup', {
text: 'Email Group Name',
onclick: function () {
editor.insertContent('{EmailGroupName}');
}
});
editor.addMenuItem('alertgroup', {
text: 'Alert Group Name',
onclick: function () {
editor.insertContent('{AlertGroupName}');
}
});
}
});
</script>
<!-- /TinyMCE -->
<body>
<form method="post">
<!-- Gets replaced with TinyMCE, remember HTML in a textarea should be encoded -->
<textarea name="tinyMceEditor" cols="1" rows="1" style="width:100%; height: 78%"></textarea>
</form>
</body>
</html>
I then have a button on my Windows Form that calls the following:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(tinyMceEditor.HtmlContent)
End Sub
I would have expected that to return the content, but instead it is only returning the full text of the function itself, which is this:
function GetContent() {
return tinyMCE.get('tinyMceEditor').getContent();
}
It is returning that in string form, of course, since that is how my code structured it. I'm not sure why it isn't returning the HTML, though.
Right now you're just referencing the GetContent function so it's returning the definition, you need to call it to get the result.
Change:
Dim html As JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent")
To:
Dim html As JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent();")
Related
I'm trying to integrate the well-known lite-youtube plugin for writing tinymce's rich text.
And I did not succeed in understanding how to integrate the plugin into the system of tinymce, I would appreciate it if someone could help me with this, thanks
This is my code:
<script>
tinymce.init({
selector: '#myTextarea',
plugins: 'anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount image',
toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table | align lineheight | numlist bullist indent outdent | emoticons charmap | removeformat | image | media',
language: 'he_IL',
image_class_list: [{title: 'Full size', value: 'great_picture_of_the_article'}],
image_dimensions: false,
link_class_list: [{title: 'Formatted link', value: 'link_to_external_site'}],
</script>
Here are the codes I tried to attach:
media_url_resolver: function (data, resolve) {
if (data.url.indexOf('YOUR_SPECIAL_VIDEO_URL') !== -1) {
var embedHtml = '<lite-youtube videoid=' + data.url +
'"></lite-youtube>';
resolve({html: embedHtml});
} else {
resolve({html: ''});
}
},
video_template_callback: function(data) {
return '<lite-youtube videoid="' + data.source1 + '"></lite-youtube>';
},
I'm having difficulty understanding what I need to do, in order for getcontent to produce html that is identical to the content displayed in the tinymce window/frame/area.
I have tried with inline and iframe, but they both return identical results.
I've started using a skin, to try and ensure the formatting/styling are applied at load, but it makes no difference.
Can anyone tell me what I need to do, so that when I view (in a web browser) the html produced by getcontent, the result is visually identical to what I'm seeing in my tinymce viewer? Notably, font settings are not being retained. (e.g. by creating a file from getcontent result and opening it in a browser).
NB.. Tinymce is being loaded from a Filemaker database (file = Notes::Notes_RTE_html)
<!DOCTYPE html>
<html lang=’en’>
<head>
<meta charset=’utf-8’>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script src='https://website/TINY/tinymce/js/tinymce/tinymce.min.js'></script>
<script>
tinymce.init({
selector: '#GSNotes',
skin_url: 'https://website/TINY/GSNotes-skin/skins/ui/GSNotes-skin/',
content_css : 'https://website/TINY/GSNotes-skin/skins/content/GSNotes-skin/content.min.css',
inline: true,
inline_styles : true,
menubar: false,
statusbar: false,
contextmenu: false,
toolbar_location: 'bottom',
paste_data_images: true,
inline_boundaries: false,
link_context_toolbar: true,
toc_depth: 4,
toc_header: 'h3',
autosave_interval: '5s',
fontsize_formats: '0.5rem 0.75rem 0.85rem 0.9rem 1rem 1.1rem 1.2rem 1.4rem 1.7rem',
insertdatetime_formats: ['✏️ (%d/%m/%Y - %Hh%M)'],
plugins: 'table image imagetools quickbars searchreplace lists link autolink autosave paste media hr codesample insertdatetime toc blockquote ',
quickbars_insert_toolbar: '',
quickbars_selection_toolbar: ' bold italic underline strikethrough | blockquote | forecolor backcolor | link | removeformat',
toolbar: 'searchreplace insertdatetime | bullist numlist | hr | paragraphgroup fontgroup insertgroup | tocupdate | print preview restoredraft ',
toolbar_groups: {
fontgroup: {
icon: 'change-case',
tooltip: 'Fonts',
items: ' fontsizeselect '
},
paragraphgroup: {
icon: 'visualchars',
tooltip: 'Paragraph format',
items: 'h1 h2 h3 h4 | toc | alignleft aligncenter alignright alignjustify | indent outdent'
},
insertgroup: {
icon: 'image',
tooltip: 'Insert',
items: 'image media table paste codesample'
}
},
codesample_languages: [
{text: 'HTML/XML', value: 'markup'},
{text: 'JavaScript', value: 'javascript'},
{text: 'CSS', value: 'css'}
],
});
</script>
</head>
<body>
<div id= 'GSNotes' style='min-height: 550px; height: 98% ; font-family: Avenir Next ; font-size: 0.9rem ; line-height: 1.2rem ; color : #1D273D ; ' > " & Notes::Notes_RTE_html & " </div>
<script>
const saveTEXT = function() {
const textPlain = tinymce.get('GSNotes').getContent({ format: 'text' });
const textHTML = tinyMCE.get('GSNotes').getContent();
const jsonARR = { textPlain, textHTML};
FileMaker.PerformScriptWithOption ( 'RTE-exit' , JSON.stringify(jsonARR) , '5' ); }
</script>
</body>
</html>
TinyMCE does not contain the attached CSS in the result of the getContent() query. The editor works a bit differently.
By default, it outputs "clean" HTML, assuming that you have some CSS on the platform that defines its' appearance. To bring content in the editor to the same appearance as it should be on the platform, the content_css parameter is used.
Thus, if you need the content to have some styling after it is exported from TinyMCE, you will need to add the same CSS on your side, after it is exported. E.g., add
<link rel="stylesheet" href="https://website/TINY/GSNotes-skin/skins/content/GSNotes-skin/content.min.css">
I am trying to open MatDialog from tinymce custom button but that matdialog is not getting initialized until I resize the window. I am aware that it is due to the method is written inside tinymce init json object and hence causing problem but don't know what to do:
in component class, I mentioned it like this:
tinyMceConfig: any = {
menubar: false, plugins: ['advlist autolink lists link image charmap print preview hr anchor pagebreak','searchreplace wordcount visualblocks visualchars code fullscreen','insertdatetime media nonbreaking save table contextmenu directionality','emoticons template paste textcolor colorpicker textpattern'], toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | code | link Upload',
setup: (editor) => {
editor.ui.registry.addButton('Upload', {
icon: 'browse',
onAction: ()=> {
const dialogRef: MatDialogRef<any> = this.dialog.open(UploadmediaComponent, { width: '95vw', disableClose: true, panelClass: 'uploadPanel'});
dialogRef.afterClosed().subscribe(res => {
if (!res) {
return;
}
res.forEach(image => {
console.log(image.url);
editor.insertContent(image.url);
});
});
}
});
}
};
In component:
<editor formControlName="popupText" name="popupText" apiKey="xxxx"
[init]="tinyMceConfig"></editor>
I had borken down this code further and separated setup part in ngOnInit but that made no difference.
Just to wrap the initialization of the dialog with ngZone.run and it worked perfect for me:
onAction: ()=> {
this.ngZone.run(()=> {
const dialogRef: MatDialogRef<any> = this.dialog.open(UploadmediaComponent, {
width: '95vw', disableClose: true, panelClass: 'uploadPanel'});
dialogRef.afterClosed().subscribe(res => {
if (!res) {
return;
}
res.forEach(image => {
editor.insertContent('<img src="' + image.url + '">');
});
});
});
}
I want to use TinyMCE in a Blazor server side app, but it shows up on page load for a second and then disappears. I blame it on StatehasChanged() thus I have written an interop function that re-initializes TinyMCE and is called in the OnAfterRender() of the page.
This is the JS interop function:
initTinyMce: function (tinyMceID) {
tinymce.init({
selector: 'textarea.tinyMce'
});
return "";
//var editor = tinyMCE.get(tinyMceID);
//if (editor && editor instanceof tinymce.Editor) {
// editor.init();
//}
}
In the OnAfterRender I call it like so:
protected override void OnAfterRender() {
base.OnAfterRender();
string a = jsInterop.InitTinyMce("myTinyMce").Result;
}
But still it disappears a second after it shows itself. How to fix this problem?
I wasn't able to get it to mimic this behaviour so maybe you were using a different version of tinyMce and Blazor. I'm using TinyMce 5.3.1 and .Net Core 3.1. I see you switched to using SyncFusion but maybe this answer will help anyone else who comes along here looking to try to do this in Blazor like I was yesterday.
I think the main thing is disposing of the element when you navigate away and re-initializing when it comes into view / focus / is loaded / etc...
Here's a component that I put together last night that should take care of all the pieces and make it bind-able like a regular <InputTextArea>
I'm new to Blazor and components. This is the second bind-able form control I've made. If anyone has any comments or suggestions on how this could be better please edit or let me know and I can update. Thanks!
In the JS block in _Host.html (i'm using a local version of TinyMCE)
<script src="/js/tinymce/tinymce.min.js"></script>
/Shared/Components/Textarea.razor
<textarea id="#Id" #bind-value="Value" #bind-value:event="onchange" />
/Shared/Components/Textarea.razor.cs:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.JSInterop;
namespace Application.Shared.Components
{
public partial class Textarea : ComponentBase, IDisposable
{
[Inject] private IJSRuntime JSRuntime { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public string Value { get; set; }
[Parameter] public EventCallback<string> ValueChanged { get; set; }
[Parameter] public string Id { get; set; } = null;
private DotNetObjectReference<Textarea> _elementRef;
[Parameter] public MenuModeEnum MenuMode { get; set; } = MenuModeEnum.standard;
protected string FieldClass => GivenEditContext?.FieldCssClass(FieldIdentifier) ?? string.Empty;
protected EditContext GivenEditContext { get; set; }
protected FieldIdentifier FieldIdentifier { get; set; }
protected string CurrentValue
{
get => Value;
set
{
var hasChanged = !EqualityComparer<string>.Default.Equals(value, Value);
if (!hasChanged) return;
_ = value;
Value = value;
_ = ValueChanged.InvokeAsync(value);
GivenEditContext?.NotifyFieldChanged(FieldIdentifier);
}
}
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
this.Id = Id ?? Guid.NewGuid().ToString();
_elementRef = DotNetObjectReference.Create(this);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("TinyMce.init", Id, Enum.GetName(typeof(MenuModeEnum), MenuMode), _elementRef);
}
}
[JSInvokable("textArea_OnChange")]
public void Change(string value)
{
CurrentValue = value;
}
protected virtual void Dispose(bool disposing)
{
JSRuntime.InvokeVoidAsync("TinyMce.dispose", Id, _elementRef);
}
void IDisposable.Dispose()
{
Dispose(disposing: true);
}
internal void DismissInstance()
{
JSRuntime.InvokeVoidAsync("TinyMce.dispose", Id, _elementRef);
StateHasChanged();
}
}
}
/Shared/Components/Enums/MenuModeEnum.cs:
namespace Application.Shared.Components.Enums
{
public enum MenuModeEnum
{
standard,
minimal,
grouped,
bloated
}
}
/wwwroot/js/site.js:
if (!window.TinyMce) {
window.TinyMce = {};
}
window.TinyMce = {
params : {
standard: {
plugins: 'code codesample link image autolink lists media paste table table spellchecker',
toolbar1: 'undo redo | paste | removeformat styleselect | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image media codesample | table | code | spellchecker',
menubar: false,
branding: false,
toolbar_mode: 'floating'
},
minimal: {
toolbar1: 'bold italic underline',
menubar: false,
branding: false,
toolbar_mode: 'floating'
},
grouped: {
plugins: "emoticons hr image link lists charmap table",
toolbar: "formatgroup paragraphgroup insertgroup",
toolbar_groups: {
formatgroup: {
icon: 'format',
tooltip: 'Formatting',
items: 'bold italic underline strikethrough | forecolor backcolor | superscript subscript | removeformat'
},
paragraphgroup: {
icon: 'paragraph',
tooltip: 'Paragraph format',
items: 'h1 h2 h3 | bullist numlist | alignleft aligncenter alignright alignjustify | indent outdent'
},
insertgroup: {
icon: 'plus',
tooltip: 'Insert',
items: 'link image emoticons charmap hr'
}
},
menubar: false,
branding: false
},
bloated: {
plugins: 'code codesample link image autolink lists media paste table table spellchecker',
toolbar1: 'undo redo | styleselect | forecolor | bold italic underline strikethrough | link image media codesample | table | code | spellchecker',
toolbar2: 'h1 h2 h3 | bullist numlist | alignleft aligncenter alignright alignjustify | outdent indent | emoticons charmap hr',
menubar: false,
branding: false,
toolbar_mode: 'floating'
}
},
init: function (id, mode, dotnetHelper) {
var params = window.TinyMce.params[mode];
params.selector = '#' + id;
params.setup = function (editor) {
editor.on('change', function (e) {
console.log($('#' + id).val());
$('#' + id).val(editor.getContent());
$('#' + id).change();
console.log($('#' + id).val());
dotnetHelper.invokeMethodAsync("textArea_OnChange", $('#' + id).val());
});
}
tinymce.init(params);
},
dispose: function (id, dotnetHelper) {
tinymce.remove('#' + id);
}
};
Usage:
<Textarea MenuMode="#MenuModeEnum.minimal" #bind-Value="#SomeObject.Comments" />
I'm not a professional programmer, I've had little experience working with Javascript and so this Tinymce is confusing me. Basically my client wants to update the content himself without touching the code, so I have to set up this Tinymce so he can edit the content directly on browser. I've followed the instruction to install Tinymce but when I click either save or submit, the editing frame refreshes itself and shows the same content without any change. Below is my code in the HTML page I want to edit. Other than this HTML page, I don't have any other php or html page. Please tell me what I need to do.
<script type="text/javascript" src="tinymce/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector: "textarea#elm1",
theme: "modern",
width: 800,
height: 600,
plugins: [
"advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
"searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
"save table contextmenu directionality emoticons template paste textcolor"
],
content_css: "css/content.css",
toolbar: "save | insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons",
style_formats: [
{title: 'Bold text', inline: 'b'},
{title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
{title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
{title: 'Example 1', inline: 'span', classes: 'example1'},
{title: 'Example 2', inline: 'span', classes: 'example2'},
{title: 'Table styles'},
{title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
],
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
});
</script>
And this is my code for the form in the same HTML page:
<form method="post" action="news.html">
<textarea id="elm1">The content I wanna edit</textarea>
<input type="submit" name="submitform" value="Sendform"></form>
Thanks very much guys!!
UPDATE 1: From the first answer and from other answers I collected from the internet, I've managed to get my code like this, but it still doesn't work, the Submit button seems do to nothing when I click on it now.
In the HTML page:
<script type="text/javascript" src="tinymce/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector: "textarea#elm1",
theme: "modern",
width: 800,
height: 600,
plugins: [
"advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
"searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
"save table contextmenu directionality emoticons template paste textcolor"
],
content_css: "css/content.css",
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons",
style_formats: [
{title: 'Bold text', inline: 'b'},
{title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
{title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
{title: 'Example 1', inline: 'span', classes: 'example1'},
{title: 'Example 2', inline: 'span', classes: 'example2'},
{title: 'Table styles'},
{title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
],
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
});
</script>
<script type="text/javascript">
$(function(){
var url = "news.php";
$("#SubmitBtn").click(function(){
//"content" will PHP variable
$.post(url, { "page_content" : tinyMCE.activeEditor.getContent() }, function(respond){
if ( respond == ){
alert('Content saved to file');
return;
} else {
//Error message assumed
alert(respond);
}
});
});
});
</script>
The form:
<form method="post" action="news.php">
<textarea id="elm1" name="page_content">The content I wanna edit</textarea>
<input type="button" id="SubmitBtn" value="Submit"/>
</form>
In the PHP page:
<?php
if ( isset($_POST['page_content']) ){
//This is what you want - HTML content from tinyMCE
//just treat this a string
if ( save_html_to_file($_POST['page_content'], 'news.html') ){
//Print 1 and exit script
die(1);
} else {
die('Couldnt write to stream');
}
}
/**
*
* #param string $content HTML content from TinyMCE editor
* #param string $path File you want to write into
* #return boolean TRUE on success
*/
function save_html_to_file($content, $path){
return (bool) file_put_contents($path, $content);
}
<form method="post" action="news.html"> - in the action property, you need to change it to a PHP file (or whatever language you'd like), where you can write a script to process the data sent. What happens is that the data from TinyMCE will be sent in a variable to the server, and you need to save it to a database of some sort. I'm assuming news.html is a static page - it will have to run off of a database for the client to update it.
Do some research on saving and reading from a MySQL database with PHP, and saving data from a form.
You'd need to give your textarea a name -- <textarea id="elm1" name="page-content">
You would then access what was submitted in PHP like this:
$content = $_POST['page-content'];
and then you can save it to a database or whatever you wish.