TinyMCE 4 Plugins: Can't get tinymce.Editor.getLang() working - javascript

I am currently switching a plugin from TinyMCE 3.x to the new version TinyMCE 4.0.26. I encountered heavy problems when trying to internationalize my plugin labels.
Within my plugin.js, I am loading the language pack by calling
tinymce.PluginManager.requireLangPack('myplugin');
with my i18n file langs/de.js looking something like this:
tinyMCE.addI18n('de', {
myplugin: {
button : 'Link einf\u00FCgen/bearbeiten',
title : 'Link einf\u00FCgen/bearbeiten'
}
});
When I access the the static context
tinymce.i18n.data.myplugin
I can see that both variables button and title are available.
THE PROBLEM:
When calling editor.getLang('myplugin.button') I get {#myplugin.button} instead of the appropriate variable value.
After I investigated the source code a little bit, I found out that it expects the language code to exist within the tinyMCE.i18n.data....., which is not available
getLang: function(name, defaultVal) {
return (
this.editorManager.i18n[(this.settings.language || 'en') + '.' + name] ||
(defaultVal !== undefined ? defaultVal : '{#' + name + '}')
);
},
#see https://github.com/tinymce/tinymce/blob/4.0.26/js/tinymce/classes/Editor.js#L1105
Have I done something wrong? Has anyone created a plugin for the new TinyMCE version and managed to get the internationalization working?

Thanks to everyone, who tried to help me out on this. Unfortunately I could not make my plugin work with translated labels in the popup window, but finally found a solution.
Everything below works perfectly okay and easy in TinyMCE version 4.2.6.
Here the steps to make everything work with a plugin named example:
Create your plugin directory at plugins/example
Create the required plugin JS file plugins/example/plugin.min.js ( take a look at the example http://pastebin.com/jEARrtWN ) - As #msqar recommended I added the requireLangPack call right before my plugin function.
Now create your translation files ( Please replace de_AT by your language code ) at langs/de_AT.js and plugins/example/langs/de_AT.js
tinymce.addI18n('de_AT', {
'Title': 'Titel',
'Example plugin': 'Beispielplugin'
});
All the ( in my example ) english labels are automatically translated to de_AT when setting up TinyMCE like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<textarea name="test"></textarea>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="tinymce/tinymce.min.js"></script>
<script>
jQuery(document).ready(function ($) {
$('textarea').tinymce({
theme: "modern",
plugins: 'example',
toolbar: 'example',
language:'de_AT'
});
})
</script>
</body>
</html>
The result
When opening the dialog using the button inside the toolbar, the window title Example plugin is automatically translated to Beispielplugin
NO special calls to editor.getLang required.
I hope this guide works for other developers around here too. I would appreciate any positive or negative feedback.
Thanks a lot to all the developers here at #stackoverflow.

You can use the following to directly access the right strings:
tinymce.EditorManager.i18n.data[ed.getParam('language') + '.myplugin_name']['my_key'];

I finally made it work, maybe it can help, this is 1 year old though, but can help others.
Since the entire way of creating a plugin changed from 3.X to 4.X, the requireLangPack i was adding to my plugin was in an incorrect location.
I was doing:
tinymce.PluginManager.add('myplugin', function(editor, url) {
tinymce.PluginManager.requireLangPack('myplugin');
...
});
When it should be:
tinymce.PluginManager.requireLangPack('myplugin');
tinymce.PluginManager.add('myplugin', function(editor, url) {
...
});
Also, the way I accessed those variables was:
editor.getLang("myplugin").title;
Instead of
editor.getLang("myplugin.title");
Hope it works for other people under the same circumstance.

Related

How to resolve type name conflict from two separate Javascript libraries?

Let me start by saying that I'm primarily a C# programmer who only extremely rarely ventures into JavaScript.
I can write myself some JS code as long as its mostly plain. I can handle jQuery and the odd self-sufficient 3rd-party library, but couldn't code myself out of a wet paper bag when React, Angular, Bootstrap and others enter the scene. I'm also not used to using npm or any other similar package manager.
It was never really my job nor interest, so I never went there. Whenever I code some JS, I reference the required JS files in my <script> tags and then use them as directly as possible.
I'm currently creating a very simple proof of concept web app which will have its client parts rebuilt by competent people sooner or later. But in the mean time I have to provide the bare-bones functionality that will serve as a rough guideline for the next team to take over, whenever that might be.
I've picked two libraries that each seem easy to use and get the job done, when used separately. But when I try to use them together on the same page, I run into a problem: they both use the same name for their main type, and I can't seem to disambiguate between them.
These are the libraries:
JSON Editor
JSON Schema Form Builder
They both declare a type named JSONEditor, which I can use as long as I don't reference both of the libraries at once.
So far I've tried to solve this by using modules and import-ing the type using different names, but it didn't work... I got a bunch of errors in the console about "import not found" and "e is not defined", which makes me think I'm tackling this wrong.
How would I solve this using plain JS if possible?
UPDATE: As suggested, I'm providing a minimal example that demonstrates my use:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Page</title>
<link href="/lib/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="editor" style="width: 300px; height: 200px;"></div>
<div id="form"></div>
</div>
<!--library 1: https://github.com/josdejong/jsoneditor -->
<script src="/lib/jsoneditor/jsoneditor.min.js"></script>
<!--library 2: https://github.com/jdorn/json-editor -->
<script src="/lib/jsonform/jsonform.min.js"></script>
<script>
// Library 1: The JSON code editor.
var editor = new JSONEditor(document.getElementById("editor"), { mode: "code" });
// Library 2: The form builder.
var form = new JSONEditor(document.getElementById("form"), {
ajax: true,
schema: {
$ref: "/api/describe/service/test"
}
});
</script>
</body>
</html>
If I comment out the use of one library (whichever), the other works as expected and the content is displayed at the respective target <div>. But if I try both at once, as shown above, nothing is displayed, and the following error is output to console:
Uncaught TypeError: t is undefined
This happens at the var editor = new JSONEditor line, which makes me think that the type from the second library overwrites the first and causes the problem.
This is understandable to me and isn't the issue per-se. The issue is that I don't know how to import the two JSONEditor types so that they can be referenced separately.
The maintainer of the code editor (JSON Editor, not JSON Schema Form Builder) has addressed and closed an issue about exactly this in the past: https://github.com/josdejong/jsoneditor/issues/270
His recommended solution is something like the following:
<script src="assets/jsoneditor/dist/jsoneditor.min.js"></script>
<script>
var JSONEditorA = JSONEditor;
</script>
<script src="assets/json-editor/dist/jsoneditor.min.js"></script>
<script>
var JSONEditorB = JSONEditor;
</script>
If you must use script tags this is probably the way to go.

javascript including math.js

I am trying to call the Math.matrix() function, and I am quite certain I am not importing the file correctly into my javascript code. I have read through the StackOverflow question "how to include and use math.js": and given that advice, I have the following :
<HTML >
<!DOCTYPE html>
<head>
<script src=https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.1/math.js>
</script>
<script type="text/javascript" >
function rotate_clockwise(){
/* code skipped */
matrix = Math.matrix(matrix, rotationmatrix);
}
</script>
</head>
<body>
</body>
</HTML>
where the cdns reference I have taken from this link
But on run when rotate_clockwise is called via slider the chrome 68 debugger states Uncaught type error : Math.matrix is not a function, so I do believe I am not including this file correctly.
My base assumption is that including a file once, in one set of script tags, is enough for any javascript function to use this library, which resides within a different set of script tags.
Thanks so much for any assistance you can provide.
I think you need math.matrix(...)--lower case math since Math is a standard JS library.

Can someone give me a step by step tutorial?

I started to use codeMirror... But i don't really understand the manual and don't find a good tutorial on the internet. At the moment, i managed to get this:
var codeHtml = $(".codemirror-html") [0];
var editor = CodeMirror.fromTextArea(codeHtml, {
mode: "htmlmixed",
lineNumbers: true
});
It does basically works, there are linenumbers and a textarea :D, but the mode doesnt' works. It's just raw black text in the textarea.
I think I importet the needed Files:
<script src="cm/lib/codemirror.js"></script>
<link rel="stylesheet" href="cm/lib/codemirror.css">
<script src="cm/mode/javascript/javascript.js"></script>
<script src="cm/mode/htmlmixed/htmlmixed.js"></script>
I don't know what I am doing wrong.. I alsow tried to add a theme, didn't worked as well.
Please, can someone show me how to do it?
The htmlmixed mode depends on the xml and css modes as well, so you'll have to add script tags for those.

Dojo: Swapping two different views in a Single Page Application

I´m new to dojo and I´m want to do the following:
Pretend you have a single page application but you have two views, which are built up totally different. One view is e.g. a startpage which would just fill the Bordercontainer-center. The second view would rather look like a standard webapp, with a header in the Bordercontainer-top, a menu in Bordercontainer-left and some content in Bordercontainer-center.
If the index.html (single page app) is now called I want the startpage to appeare first. There should be an onclick-event in it. With this event the views should change. This means the startpage disappears and the second webapp-view is shown.
What would be the best way to implement this?
I thought of using two Bordercontainers.
The first Bordercontainer would contain the startpage in the region center.
The second Bordercontainer would contain the webapp-view (top, left, center).
Would it now be possible to swap the center region from the frist Bordercontainer in a way that the startpage get´s swaped with the second Bordercontainer? Would this be a way how to solve my approach?
If yes I would need some kind of controller which would swap the view.
Could I solve this with using dojo.wire?
Or is there a straight forward approach in dojo, which I have not found yet?
If there is a small example or tutorial out there, it would be great to receive a link to it.
Thx for every hint.
You should take a look at dojox/mobile (http://dojotoolkit.org/reference-guide/1.10/dojox/mobile.html) it has support for what you are trying to do. You could also look at dojox/app (http://dojotoolkit.org/reference-guide/1.10/dojox/app.html or http://dojotoolkit.org/documentation/tutorials/1.9/dojox_app/contactsList/) to see if that gives you what you need.
I tried the following code:
require([
"dijit/form/Button"
], function() {
changeView = function(idShow, idHide) {
var showView = dojo.byId(idShow);
var showHide = dojo.byId(idHide);
if (showView.style.display == 'block') {
showView.style.display = 'none';
showHide.style.display = 'block';
} else {
showView.style.display = 'block';
showHide.style.display = 'none';
}
};
});
#view1 {
display: block;
}
#view2 {
display: none;
}
<html>
<head>
<title>Change View</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/resources/dojo.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dijit/themes/tundra/tundra.css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js" data-dojo-config="isDebug: true, parseOnLoad: true"></script>
</head>
<body class="tundra">
<div id="view1">
View1
<br>
<button dojoType="Button" widgetId="view1Button" onClick="changeView('view2', 'view1');">Change to View2</button>
</div>
<div id="view2">
View2
<br>
<button dojoType="Button" widgetId="view2Button" onClick="changeView('view1', 'view2');">Change to View1</button>
</div>
</body>
</html>
This lets me change the view with onclick and css and a little js.
I think this is one of the various ways you mentioned, for solving my approach. But what I think I´m missing now is to combine my function changeView with dojo - somehow.
What would be the right way to combine dojo and the function changeView now?
Would I write a dojo modul with a define and then work with it in my html and calling it with require?
Or generally.. for a dojo-beginner.
If I need javascript code for my app, is there a straight forward way to combine this with dojo?
e.g. with any kind of approuch
Look an see what dojo has to solve the approch
Write JS code, if there is no suitable dojo modul/function yet
Think about seperating the JS code it into modules
Write the modules in dojo with define
Use the modules in the app by calling them with require
Would this be a proper way for programming in dojo?
The question is more a general "howto glue JS and dojo together" to write webapps and use the advantages of dojo.
Thx in advance.

Trying to add Qooxdoo widgets in html page

As a proof of concept I would like to show the some Qooxdoo widgets (which i find pretty nice) in a very simple index.html file.
Here I try to show a button :
<head>
<title>Title</title>
<script type="text/javascript" src="http://demo.qooxdoo.org/3.5/framework/q-3.5.min.js"></script>
<script>
var button = new qx.ui.form.Button("Hello...");
this.getRoot().add(button, {left: 30, top: 20});
</script>
</head>
If I run the above I get this :
Uncaught ReferenceError: qx is not defined
Is my library link correct? Or is it even possible to link qooxdoo javascript in a HTML file? We already have a large established javascript application, and we would like to just drop in qooxdoo widgets that we like. Not sure if that is possible though.
You are including the qx.Website library and try to use qx.Desktop widgets. That ain't gonna work. Either you choose qx.Desktop and use the inline app approach [1] or you use the qx.Website widgets [2].
[1] http://manual.qooxdoo.org/current/pages/development/skeletons.html#inline
[2] http://demo.qooxdoo.org/devel/website-api/index.html#Accordion

Categories

Resources