prevent filter selected text dialog from opening in wrong order

This commit is contained in:
Andrew Dolgov 2021-02-20 21:07:28 +03:00
parent 590b1fc39e
commit da97b29dbe
3 changed files with 313 additions and 324 deletions

View File

@ -358,6 +358,27 @@ const App = {
return p; return p;
} }
}, },
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
getSelectedText: function() {
let text = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const container = document.createElement("div");
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().textText;
}
}
return text.stripTags();
},
displayIfChecked: function(checkbox, elemId) { displayIfChecked: function(checkbox, elemId) {
if (checkbox.checked) { if (checkbox.checked) {
Element.show(elemId); Element.show(elemId);

View File

@ -19,8 +19,6 @@ const Filters = {
query = {op: "pref-filters", method: "edit", id: id}; query = {op: "pref-filters", method: "edit", id: id};
} }
xhr.post("backend.php", query, function (reply) {
try {
const dialog = new fox.SingleUseDialog({ const dialog = new fox.SingleUseDialog({
id: "filterEditDlg", id: "filterEditDlg",
title: id ? __("Edit Filter") : __("Create Filter"), title: id ? __("Edit Filter") : __("Create Filter"),
@ -307,54 +305,46 @@ const Filters = {
}); });
} }
}, },
content: reply content: __("Loading, please wait...")
}); });
if (!App.isPrefs()) { const tmph = dojo.connect(dialog, 'onShow', function () {
/* global getSelectionText */ dojo.disconnect(tmph);
const selectedText = getSelectionText();
const lh = dojo.connect(dialog, "onShow", function () { xhr.post("backend.php", query, function (reply) {
dojo.disconnect(lh); dialog.attr('content', reply);
if (!App.isPrefs()) {
const selectedText = App.getSelectedText();
if (selectedText != "") { if (selectedText != "") {
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) : const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive(); Feeds.getActive();
const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1}; const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1};
dialog.editRule(null, dojo.toJson(rule)); dialog.editRule(null, dojo.toJson(rule));
} else { } else {
const query = {op: "article", method: "getmetadatabyid", id: Article.getActive()}; const query = {op: "article", method: "getmetadatabyid", id: Article.getActive()};
xhr.json("backend.php", query, (reply) => { xhr.json("backend.php", query, (reply) => {
let title = false; let title;
if (reply && reply.title) title = reply.title; if (reply && reply.title) title = reply.title;
if (title || Feeds.getActive() || Feeds.activeIsCat()) { if (title || Feeds.getActive() || Feeds.activeIsCat()) {
console.log(title + " " + Feeds.getActive()); console.log(title + " " + Feeds.getActive());
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) : const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive(); Feeds.getActive();
const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1}; const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1};
dialog.editRule(null, dojo.toJson(rule)); dialog.editRule(null, dojo.toJson(rule));
} }
}); });
} }
});
} }
});
});
dialog.show(); dialog.show();
} catch (e) {
App.Error.report(e);
}
});
}, },
}; };

View File

@ -401,25 +401,3 @@ const Notify = {
} }
}; };
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
/* exported getSelectionText */
function getSelectionText() {
let text = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const container = document.createElement("div");
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().textText;
}
}
return text.stripTags();
}