More experimenting with web text box

This commit is contained in:
Nikolaj Olsson 2020-11-23 20:22:16 +01:00
parent e787b83ad2
commit 15b0dfe15b
4 changed files with 151 additions and 8 deletions

1
.gitignore vendored
View File

@ -43,3 +43,4 @@ SubtitleEditBeta
/src/ui/packages /src/ui/packages
/src/ui/ILRepack* /src/ui/ILRepack*
/src/ui/NHunspell* /src/ui/NHunspell*
/build_beta.bat

View File

@ -1193,7 +1193,7 @@ namespace Nikse.SubtitleEdit.Controls
Color c; Color c;
if (color.StartsWith("rgb(", StringComparison.Ordinal)) if (color.StartsWith("rgb(", StringComparison.Ordinal))
{ {
string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]));
} }
else else

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -41,10 +42,22 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
var assembly = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetExecutingAssembly();
BringToFront(); BringToFront();
LoadHtml(assembly);
IsWebBrowserContextMenuEnabled = false;
AllowWebBrowserDrop = false;
ObjectForScripting = this;
}
private void LoadHtml(Assembly assembly)
{
using (var stream = assembly.GetManifestResourceStream("Nikse.SubtitleEdit.Controls.WebBrowser.WebBrowserEditBox.html")) using (var stream = assembly.GetManifestResourceStream("Nikse.SubtitleEdit.Controls.WebBrowser.WebBrowserEditBox.html"))
using (var reader = new StreamReader(stream)) using (var reader = new StreamReader(stream))
{ {
var s = reader.ReadToEnd(); var s = reader.ReadToEnd();
s = s.Replace("color: brown;", "color: " + ColorTranslator.ToHtml(Configuration.Settings.General.SubtitleFontColor) + ";");
s = s.Replace("background-color: lightblue;", "background-color: " + ColorTranslator.ToHtml(Configuration.Settings.General.SubtitleBackgroundColor) + ";");
DocumentText = s; DocumentText = s;
} }
@ -56,10 +69,6 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
Application.DoEvents(); Application.DoEvents();
Thread.Sleep(5); Thread.Sleep(5);
} }
IsWebBrowserContextMenuEnabled = false;
AllowWebBrowserDrop = false;
ObjectForScripting = this;
} }
public override string Text public override string Text
@ -124,7 +133,8 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
Document.InvokeScript("setTextDirection", new object[] { align, dir }); Document.InvokeScript("setTextDirection", new object[] { align, dir });
} }
Document.InvokeScript("setText", new object[] { value }); UpdateSyntaxColor(value);
//Document.InvokeScript("setText", new object[] { value });
} }
} }
} }
@ -333,5 +343,116 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
public void ClearUndo() public void ClearUndo()
{ {
} }
private string _lastHtml = null;
public void UpdateSyntaxColor(string text)
{
var html = HighLightHtml(text);
if (html.Contains("\n"))
{
html = html.Replace("\r\n\r\n", "<br />");
html = html.Replace("\r\n", "<br />");
html = html.Replace("\n", "<br />");
}
if (html != _lastHtml && Document != null)
{
Document.InvokeScript("setHtml", new object[] { html });
_lastHtml = html;
}
}
public string HighLightHtml(string text)
{
if (text == null)
{
return string.Empty;
}
bool htmlTagOn = false;
bool htmlTagFontOn = false;
int htmlTagStart = -1;
bool assaTagOn = false;
bool assaPrimaryColorTagOn = false;
bool assaSecondaryColorTagOn = false;
bool assaBorderColorTagOn = false;
bool assaShadowColorTagOn = false;
var assaTagStart = -1;
int tagOn = -1;
var textLength = text.Length;
int i = 0;
var sb = new StringBuilder();
while (i < textLength)
{
var ch = text[i];
if (assaTagOn)
{
if (ch == '}' && tagOn >= 0)
{
assaTagOn = false;
sb.Append($"<font color=\"{ColorTranslator.ToHtml(Configuration.Settings.General.SubtitleTextBoxAssColor)}\">");
var inner = text.Substring(assaTagStart, i - assaTagStart + 1);
sb.Append(WebUtility.HtmlEncode(inner));
sb.Append("</font>");
assaTagStart = -1;
}
}
else if (htmlTagOn)
{
if (ch == '>' && tagOn >= 0)
{
htmlTagOn = false;
sb.Append($"<font color=\"{ColorTranslator.ToHtml(Configuration.Settings.General.SubtitleTextBoxHtmlColor)}\">");
var inner = text.Substring(htmlTagStart, i - htmlTagStart + 1);
sb.Append(WebUtility.HtmlEncode(inner));
sb.Append("</font>");
htmlTagStart = -1;
}
}
else if (ch == '{' && i < textLength - 1 && text[i + 1] == '\\' && text.IndexOf('}', i) > 0)
{
var s = text.Substring(i);
assaTagOn = true;
tagOn = i;
assaTagStart = i;
}
else if (ch == '<')
{
var s = text.Substring(i);
if (s.StartsWith("<i>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("<b>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("<u>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("</i>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("</b>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("</u>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("<box>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("</box>", StringComparison.OrdinalIgnoreCase) ||
s.StartsWith("</font>", StringComparison.OrdinalIgnoreCase) ||
(s.StartsWith("<font ", StringComparison.OrdinalIgnoreCase) &&
text.IndexOf("</font>", i, StringComparison.OrdinalIgnoreCase) > 0))
{
htmlTagOn = true;
htmlTagStart = i;
htmlTagFontOn = s.StartsWith("<font ", StringComparison.OrdinalIgnoreCase);
tagOn = i;
}
else
{
sb.Append(WebUtility.HtmlEncode(ch.ToString()));
}
}
else
{
sb.Append(WebUtility.HtmlEncode(ch.ToString()));
}
i++;
}
return sb.ToString();
}
} }
} }

View File

@ -21,6 +21,7 @@
font-family: Tahoma; font-family: Tahoma;
font-weight: bold; font-weight: bold;
color: brown; color: brown;
background-color: lightblue;
/* border: 2px blue; /* border: 2px blue;
border-style: solid;*/ border-style: solid;*/
margin: 0; margin: 0;
@ -67,6 +68,17 @@
return document.getElementById(id); return document.getElementById(id);
} }
var millisecondsLastActivity = 0;
function syntaxColorIfNotActive() {
setTimeout(function () {
var ms = Date.now();
if (ms - millisecondsLastActivity > 100) {
window.external.UpdateSyntaxColor(getById("myContent").innerText);
}
}, 150);
millisecondsLastActivity = Date.now();
}
function initEvents() { function initEvents() {
getById("myContent").addEventListener("focus", function (e) { getById("myContent").addEventListener("focus", function (e) {
window.external.ClientFocus(); window.external.ClientFocus();
@ -82,10 +94,12 @@
getById("myContent").addEventListener("keyup", function (e) { getById("myContent").addEventListener("keyup", function (e) {
window.external.ClientKeyUp(e.keyCode, e.ctrlKey, e.shiftKey, e.altKey); window.external.ClientKeyUp(e.keyCode, e.ctrlKey, e.shiftKey, e.altKey);
syntaxColorIfNotActive();
}); });
getById("myContent").addEventListener("click", function (e) { getById("myContent").addEventListener("click", function (e) {
window.external.ClientClick(); window.external.ClientClick();
syntaxColorIfNotActive();
}); });
getById("myContent").addEventListener("mousemove", function (e) { getById("myContent").addEventListener("mousemove", function (e) {
@ -149,7 +163,8 @@
idx += 2; idx += 2;
} else if (node.nodeType === 3) { // #text } else if (node.nodeType === 3) { // #text
if (node.nodeValue.indexOf("\n") >= 0) { if (node.nodeValue.indexOf("\n") >= 0) {
getById("div1").innerText = getById("div1").innerText + "\ntext with new-line found!"; //ext with new-line found!";
idx += node.length;
} else { } else {
idx += node.length; idx += node.length;
} }
@ -299,6 +314,12 @@
document.getElementById("myContent").setAttribute('class', 'disabled'); document.getElementById("myContent").setAttribute('class', 'disabled');
} }
} }
function setHtml(newHtml) {
var r = getCursorPosition();
document.getElementById("myContent").innerHTML = newHtml;
setCursorPosition(r);
}
</script> </script>
</body> </body>
</html> </html>