Disable syntax-coloring per default

This commit is contained in:
Nikolaj Olsson 2020-11-26 11:50:22 +01:00
parent 2003cd99fd
commit 29a5260866
5 changed files with 209 additions and 233 deletions

View File

@ -1187,7 +1187,7 @@ $HorzAlign = Center
SubtitleTextBoxFontSize = 12;
SubtitleListViewFontSize = 10;
SubtitleTextBoxSyntaxColor = true;
SubtitleTextBoxSyntaxColor = false;
SubtitleTextBoxHtmlColor = Color.CornflowerBlue;
SubtitleTextBoxAssColor = Color.BlueViolet;
SubtitleTextBoxFontBold = true;

View File

@ -38,7 +38,7 @@ namespace Nikse.SubtitleEdit.Controls
Initialize(Configuration.Settings.General.SubtitleTextBoxSyntaxColor);
}
private bool _useWebBrowser = true;
private bool _useWebBrowser = false;
public void Initialize(bool useSyntaxColoring)
{
ContextMenuStrip oldContextMenuStrip = null;

View File

@ -15,6 +15,9 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
{
private bool _rightToLeft;
private bool _center;
private long _lastKeyOrClick = -1;
private bool _lastKeyOrClickActivity;
private System.Windows.Forms.Timer _timerSyntaxColor;
public new event EventHandler TextChanged;
public new event KeyEventHandler KeyDown;
@ -35,6 +38,9 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
Application.DoEvents();
Document?.InvokeScript("initEvents");
};
_timerSyntaxColor = new System.Windows.Forms.Timer { Interval = 100 };
_timerSyntaxColor.Tick += (sender, args) => { UpdateSyntaxColorFromJs(); };
_timerSyntaxColor.Start();
}
public void Initialize()
@ -81,37 +87,7 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
}
var text = (Document.InvokeScript("getText") ?? string.Empty).ToString();
var sb = new StringBuilder();
int max = text.Length;
int i = 0;
var isNewLine = false;
while (i < max)
{
var ch = text[i];
if (ch == '\r')
{
//continue
}
else if (ch == '\n')
{
if (!isNewLine)
{
sb.Append(Environment.NewLine);
}
isNewLine = !isNewLine;
}
else
{
sb.Append(ch);
isNewLine = false;
}
i++;
}
var result = sb.ToString();
return result;
return text;
}
set
{
@ -304,6 +280,8 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
KeyUp?.Invoke(this, new KeyEventArgs(keyData));
TextChanged?.Invoke(this, new KeyEventArgs(0));
_lastKeyOrClick = DateTime.UtcNow.Ticks;
_lastKeyOrClickActivity = true;
}
public void ClientClick()
@ -311,6 +289,8 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
var mp = PointToClient(MousePosition);
MouseClick?.Invoke(this, new MouseEventArgs(MouseButtons.Left, 1, mp.X, mp.Y, 1));
TextChanged?.Invoke(this, new KeyEventArgs(0));
_lastKeyOrClick = DateTime.UtcNow.Ticks;
_lastKeyOrClickActivity = true;
}
public void ClientMouseMove()
@ -347,14 +327,9 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
public void UpdateSyntaxColor(string text)
{
_lastKeyOrClick = DateTime.UtcNow.Ticks;
_lastKeyOrClickActivity = false;
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 });
@ -362,92 +337,123 @@ namespace Nikse.SubtitleEdit.Controls.WebBrowser
}
}
public string HighLightHtml(string text)
public void UpdateSyntaxColorFromJs()
{
if (text == null)
if (!_lastKeyOrClickActivity)
{
return string.Empty;
return; // exit if not key or click activity
}
var diff = DateTime.UtcNow.Ticks - _lastKeyOrClick;
if (diff < 10_000 * 750)
{
return; // exit if activity within the last 500 ms
}
_lastKeyOrClick = DateTime.UtcNow.Ticks;
var text = Text;
var html = HighLightHtml(text);
if (html != _lastHtml && Document != null)
{
//var oldHtml = (Document.InvokeScript("getHtml") ?? "0").ToString();
Document.InvokeScript("setHtml", new object[] { html });
_lastHtml = html;
}
}
public string HighLightHtml(string text)
{
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 lines = text.SplitToLines();
var multiLine = lines.Count > 1;
foreach (var line in lines)
{
var ch = text[i];
if (assaTagOn)
int i = 0;
var textLength = line.Length;
if (multiLine)
{
if (ch == '}' && tagOn >= 0)
sb.Append("<p>");
}
while (i < textLength)
{
var ch = line[i];
if (assaTagOn)
{
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;
if (ch == '}' && tagOn >= 0)
{
assaTagOn = false;
sb.Append("<font color=\"#4444dd\">");
var inner = line.Substring(assaTagStart, i - assaTagStart + 1);
sb.Append(WebUtility.HtmlEncode(inner));
sb.Append("</font>");
assaTagStart = -1;
}
}
}
else if (htmlTagOn)
{
if (ch == '>' && tagOn >= 0)
else if (htmlTagOn)
{
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;
if (ch == '>' && tagOn >= 0)
{
htmlTagOn = false;
sb.Append("<font color=\"#44dd44\">");
var inner = line.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))
else if (ch == '{' && i < textLength - 1 && line[i + 1] == '\\' && line.IndexOf('}', i) > 0)
{
htmlTagOn = true;
htmlTagStart = i;
htmlTagFontOn = s.StartsWith("<font ", StringComparison.OrdinalIgnoreCase);
assaTagOn = true;
tagOn = i;
assaTagStart = i;
}
else if (ch == '<')
{
var s = line.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) &&
line.IndexOf("</font>", i, StringComparison.OrdinalIgnoreCase) > 0))
{
htmlTagOn = true;
htmlTagStart = i;
tagOn = i;
}
else
{
sb.Append(WebUtility.HtmlEncode(ch.ToString()));
}
}
else
{
sb.Append(WebUtility.HtmlEncode(ch.ToString()));
}
}
else
{
sb.Append(WebUtility.HtmlEncode(ch.ToString()));
}
i++;
i++;
}
if (line == string.Empty && multiLine)
{
sb.Append("<br>");
}
if (multiLine)
{
sb.Append("</p>");
}
}
return sb.ToString();

View File

@ -68,16 +68,11 @@
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 syntaxColorIfNotActive() {
// setTimeout(function () {
// window.external.UpdateSyntaxColorFromJs(getText(), document.getElementById("myContent").innerHTML);
// }, 1);
//}
function initEvents() {
getById("myContent").addEventListener("focus", function (e) {
@ -94,12 +89,12 @@
getById("myContent").addEventListener("keyup", function (e) {
window.external.ClientKeyUp(e.keyCode, e.ctrlKey, e.shiftKey, e.altKey);
syntaxColorIfNotActive();
// syntaxColorIfNotActive();
});
getById("myContent").addEventListener("click", function (e) {
window.external.ClientClick();
syntaxColorIfNotActive();
// syntaxColorIfNotActive();
});
getById("myContent").addEventListener("mousemove", function (e) {
@ -107,99 +102,72 @@
});
}
function getPreviousOrParentLastChild(node) {
if (node.previousSibling) {
return [node.previousSibling, 0];
}
if (node.parentNode && node.parentNode.id === "myContent") {
return [null, 0];
}
var extraChar = 0;
if (node && node.parentNode.previousSibling) {
if (node.parentNode.previousSibling.lastChild) {
if (node.parentNode.nodeName === "P") {
if (node.parentNode.parentNode.id === "myContent" && !node.parentNode.previousSibling) {
// skip first P
} else {
getById("div1").innerText = getById("div1").innerText + "\nP found1";
extraChar = 2;
}
function getLengthFromChildren(node, txtObj, top) {
while (node != null) {
if (node.firstChild) {
if (node.nodeName === "P" || node.nodeName === "DIV") {
txtObj.l += 2;
}
return [node.parentNode.previousSibling.lastChild, extraChar];
}
return [node.parentNode.previousSibling, extraChar];
} else if (node.parentNode.parentNode && node.parentNode.parentNode.previousSibling) {
if (node.parentNode.parentNode.previousSibling.lastChild) {
if (node.parentNode.parentNode.nodeName === "P") {
if (node.parentNode.parentNode.parentNode.id === "myContent" && !node.parentNode.parentNode.previousSibling) {
// skip first P
} else {
// getById("div1").innerText = getById("div1").innerText + "\nP found1";
extraChar = 2;
}
getLengthFromChildren(node.firstChild, txtObj, false);
} else {
if (node.nodeType === 3) {
txtObj.l += node.nodeValue.length;
} else if (node.nodeName === "P" || node.nodeName === "DIV") {
txtObj.l += 2;
} else if (node.nodeName === "BR" && node.parentNode.nodeName !== "P" && node.parentNode.nodeName !== "DIV") {
txtObj.l += 2;
} else if (node.innerHTML === "<br>" && node.nodeName !== "P" && node.nodeName !== "DIV") {
txtObj.l += 2;
}
return [node.parentNode.parentNode.previousSibling.lastChild, extraChar];
}
return [node.parentNode.parentNode.previousSibling, extraChar];
if (top) {
return;
}
node = node.nextSibling;
}
return [null, extraChar];
return;
}
function getNodePosition(node, offset) {
function getNodePosition(node, txtObj) {
if (!node) {
return 0;
return txtObj.l;
}
getById("div1").innerText = "getNodePosition for : " + getById("myContent").innerHTML;
var idx = offset;
if (node.nodeName === "P" && node.previousSibling) {
idx += 2;
getById("div1").innerText = getById("div1").innerText + "\nStart with P";
}
var res = getPreviousOrParentLastChild(node);
node = res[0];
idx += res[1];
var i = 0;
while (node && i < 100) {
i++;
if (node.nodeName === "BR") {
getById("div1").innerText = getById("div1").innerText + "\nBR found!";
idx += 2;
} else if (node.nodeType === 3) { // #text
if (node.nodeValue.indexOf("\n") >= 0) {
//ext with new-line found!";
idx += node.length;
} else {
idx += node.length;
while (true) {
if (node.previousSibling) {
node = node.previousSibling;
} else {
var p = node.parentNode;
if (p.id === "myContent") {
return txtObj.l;
}
if (p.previousSibling) {
node = p.previousSibling;
} else {
p = node.parentNode.parentNode;
if (p && p.id === "myContent" || (p && p.nodeName === "P" && p.parentNode && p.parentNode.id === "myContent")) {
return txtObj.l;
}
if (p && p.parentNode && p.parentNode.previousSibling) {
node = p.parentNode.previousSibling;
} else {
return txtObj.l;
}
}
} else if (node.innerText) {
idx += node.innerText.length;
}
var resInner = getPreviousOrParentLastChild(node);
node = resInner[0];
idx += resInner[1];
getLengthFromChildren(node, txtObj, true);
}
return idx;
}
function getCursorPosition() {
var x = window.getSelection();
return getNodePosition(x.focusNode, x.focusOffset);
return getNodePosition(x.focusNode, { l: x.focusOffset });
}
function getSelectionText() {
return window.getSelection().toString();
}
@ -209,41 +177,6 @@
return getNodePosition(x.anchorNode, x.anchorOffset);
}
function SetCaretPosition(el, pos) {
// Loop through all child nodes
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if (node.nodeType === 3) { // we have a text node
if (node.length >= pos) {
var range = document.createRange(),
sel = window.getSelection();
range.setStart(node, pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
return -1; // we are done
} else {
pos -= node.length;
}
} else {
pos = SetCaretPosition(node, pos);
if (pos === -1) {
return -1; // no need to finish the for loop
}
}
}
return pos;
}
function selectElementContents(el) {
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
function getNextNode(node) {
if (node.firstChild) {
return node.firstChild;
@ -255,14 +188,18 @@
if (node.parentNode && node.parentNode.id !== "myContent" && node.parentNode.nextSibling) {
if (node.parentNode.nextSibling.firstChild) {
if (node.parentNode.nodeName === "P") {
}
return node.parentNode.nextSibling;
}
return node.parentNode.nextSibling;
}
if (node.parentNode && node.parentNode && node.parentNode.parentNode.id !== "myContent" &&
node.parentNode.parentNode.nextSibling) {
if (node.parentNode.parentNode.nextSibling.firstChild) {
return node.parentNode.parentNode.nextSibling;
}
}
return null;
}
@ -312,8 +249,35 @@
element.setAttribute("dir", dir);
}
function GetTextRecur(node, txtObj, first) {
while (node != null) {
if (node.firstChild) {
if (!first && (node.nodeName === "P" || node.nodeName === "DIV")) {
txtObj.v += "\r\n";
}
GetTextRecur(node.firstChild, txtObj, false);
} else {
if (node.nodeType === 3) {
txtObj.v += node.nodeValue;
} else if (node.nodeName === "P" || node.nodeName === "DIV") {
txtObj.v += "\r\n";
} else if (node.nodeName === "BR" && node.parentNode.nodeName !== "P" && node.parentNode.nodeName !== "DIV") {
txtObj.v += "\r\n";
} else if (node.innerHTML === "<br>" && node.nodeName !== "P" && node.nodeName !== "DIV") {
if (node.parentNode && node.parentNode.nodeName !== "P" && node.parentNode.nodeName !== "DIV") {
txtObj.v += "\r\n";
}
}
}
first = false;
node = node.nextSibling;
}
return txtObj.v;
}
function getText() {
return document.getElementById("myContent").innerText;
var text = GetTextRecur(getById("myContent").firstChild, { v: "" }, true);
return text;
}
function setText(text) {
@ -340,6 +304,10 @@
document.getElementById("myContent").innerHTML = newHtml;
setCursorPosition(r);
}
function getHtml() {
return document.getElementById("myContent").innerHTML;
}
</script>
</body>
</html>

View File

@ -25285,16 +25285,18 @@ namespace Nikse.SubtitleEdit.Forms
private void UpdatePositionAndTotalLength(Label lineTotal, SETextBox textBox)
{
if (string.IsNullOrEmpty(textBox.Text))
var text = textBox.Text;
if (string.IsNullOrEmpty(text))
{
lineTotal.Text = string.Empty;
return;
}
int extraNewLineLength = Environment.NewLine.Length - 1;
int lineBreakPos = textBox.Text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
int lineBreakPos = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
int pos = textBox.SelectionStart;
var s = HtmlUtil.RemoveHtmlTags(textBox.Text, true).Replace(Environment.NewLine, string.Empty); // we don't count new line in total length... correct?
var s = HtmlUtil.RemoveHtmlTags(text, true).Replace(Environment.NewLine, string.Empty); // we don't count new line in total length... correct?
int totalLength = s.CountCharacters(false, Configuration.Settings.General.IgnoreArabicDiacritics);
string totalL;
@ -25315,7 +25317,7 @@ namespace Nikse.SubtitleEdit.Forms
return;
}
int secondLineBreakPos = textBox.Text.IndexOf(Environment.NewLine, lineBreakPos + 1, StringComparison.Ordinal);
int secondLineBreakPos = text.IndexOf(Environment.NewLine, lineBreakPos + 1, StringComparison.Ordinal);
if (secondLineBreakPos < 0 || pos <= secondLineBreakPos + extraNewLineLength)
{
lineTotal.Text = "2," + (pos - (lineBreakPos + extraNewLineLength)) + totalL;
@ -25323,7 +25325,7 @@ namespace Nikse.SubtitleEdit.Forms
return;
}
int thirdLineBreakPos = textBox.Text.IndexOf(Environment.NewLine, secondLineBreakPos + 1, StringComparison.Ordinal);
int thirdLineBreakPos = text.IndexOf(Environment.NewLine, secondLineBreakPos + 1, StringComparison.Ordinal);
if (thirdLineBreakPos < 0 || pos < thirdLineBreakPos + (extraNewLineLength * 2))
{
lineTotal.Text = "3," + (pos - (secondLineBreakPos + extraNewLineLength)) + totalL;
@ -25331,7 +25333,7 @@ namespace Nikse.SubtitleEdit.Forms
return;
}
int forthLineBreakPos = textBox.Text.IndexOf(Environment.NewLine, thirdLineBreakPos + 1, StringComparison.Ordinal);
int forthLineBreakPos = text.IndexOf(Environment.NewLine, thirdLineBreakPos + 1, StringComparison.Ordinal);
if (forthLineBreakPos < 0 || pos < forthLineBreakPos + (extraNewLineLength * 3))
{
lineTotal.Text = "4," + (pos - (thirdLineBreakPos + extraNewLineLength)) + totalL;