2023-05-31 18:32:33 +08:00
|
|
|
|
/**
|
|
|
|
|
* Widget: Show highlighted text in the right pane
|
|
|
|
|
*
|
|
|
|
|
* By design there's no support for nonsensical or malformed constructs:
|
|
|
|
|
* - For example, if there is a formula in the middle of the highlighted text, the two ends of the formula will be regarded as two entries
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import attributeService from "../services/attributes.js";
|
|
|
|
|
import RightPanelWidget from "./right_panel_widget.js";
|
|
|
|
|
import options from "../services/options.js";
|
|
|
|
|
import OnClickButtonWidget from "./buttons/onclick_button.js";
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
const TPL = `<div class="highlighted-text-widget">
|
2023-05-31 18:32:33 +08:00
|
|
|
|
<style>
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.highlighted-text-widget {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
padding: 10px;
|
|
|
|
|
contain: none;
|
|
|
|
|
overflow: auto;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.highlighted-text > ol {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
padding-left: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.highlighted-text li {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
cursor: pointer;
|
|
|
|
|
margin-bottom: 3px;
|
|
|
|
|
text-align: justify;
|
|
|
|
|
text-justify: distribute;
|
2023-06-03 14:43:20 +08:00
|
|
|
|
word-wrap: break-word;
|
|
|
|
|
hyphens: auto;
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.highlighted-text li:hover {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.close-highlighted-text {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
position: absolute;
|
|
|
|
|
top: 2px;
|
|
|
|
|
right: 2px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
2023-05-31 21:47:43 +08:00
|
|
|
|
<span class="highlighted-text"></span>
|
2023-05-31 18:32:33 +08:00
|
|
|
|
</div>`;
|
|
|
|
|
|
2023-06-03 17:00:15 +08:00
|
|
|
|
export default class HighlightedTextWidget extends RightPanelWidget {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.closeHltButton = new CloseHltButton();
|
|
|
|
|
this.child(this.closeHltButton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get widgetTitle() {
|
|
|
|
|
return "Highlighted Text";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
|
return super.isEnabled()
|
|
|
|
|
&& this.note.type === 'text'
|
2023-05-31 21:47:43 +08:00
|
|
|
|
&& !this.noteContext.viewScope.highlightedTextTemporarilyHidden
|
2023-05-31 18:32:33 +08:00
|
|
|
|
&& this.noteContext.viewScope.viewMode === 'default';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async doRenderBody() {
|
|
|
|
|
this.$body.empty().append($(TPL));
|
2023-05-31 21:47:43 +08:00
|
|
|
|
this.$hlt = this.$body.find('.highlighted-text');
|
|
|
|
|
this.$body.find('.highlighted-text-widget').append(this.closeHltButton.render());
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async refreshWithNote(note) {
|
2023-06-04 16:02:30 +08:00
|
|
|
|
/*The reason for adding highlightedTextPreviousVisible is to record whether the previous state of the highlightedText is hidden or displayed,
|
2023-06-01 20:17:00 +08:00
|
|
|
|
* and then let it be displayed/hidden at the initial time.
|
|
|
|
|
* If there is no such value, when the right panel needs to display toc but not highlighttext, every time the note content is changed,
|
|
|
|
|
* highlighttext Widget will appear and then close immediately, because getHlt function will consume time*/
|
2023-06-04 16:02:30 +08:00
|
|
|
|
if (this.noteContext.viewScope.highlightedTextPreviousVisible == true) {
|
2023-06-01 20:17:00 +08:00
|
|
|
|
this.toggleInt(true);
|
|
|
|
|
} else {
|
|
|
|
|
this.toggleInt(false);
|
|
|
|
|
}
|
2023-05-31 21:47:43 +08:00
|
|
|
|
const hltLabel = note.getLabel('hideHighlightWidget');
|
2023-05-31 18:32:33 +08:00
|
|
|
|
|
2023-06-03 11:55:50 +08:00
|
|
|
|
const optionsHlt = JSON.parse(options.get('highlightedText'));
|
2023-06-01 20:38:55 +08:00
|
|
|
|
|
2023-06-03 11:55:50 +08:00
|
|
|
|
if (hltLabel?.value == "" || hltLabel?.value === "true" || optionsHlt == "") {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
this.toggleInt(false);
|
|
|
|
|
this.triggerCommand("reEvaluateRightPaneVisibility");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 20:17:00 +08:00
|
|
|
|
let $hlt = "", hltLiCount = -1;
|
2023-05-31 18:32:33 +08:00
|
|
|
|
// Check for type text unconditionally in case alwaysShowWidget is set
|
|
|
|
|
if (this.note.type === 'text') {
|
|
|
|
|
const { content } = await note.getNoteComplement();
|
2023-06-03 11:55:50 +08:00
|
|
|
|
({ $hlt, hltLiCount } = await this.getHlt(content, optionsHlt));
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
this.$hlt.html($hlt);
|
2023-06-01 20:17:00 +08:00
|
|
|
|
if ([undefined, "false"].includes(hltLabel?.value) && hltLiCount > 0) {
|
|
|
|
|
this.toggleInt(true);
|
2023-06-04 16:02:30 +08:00
|
|
|
|
this.noteContext.viewScope.highlightedTextPreviousVisible = true;
|
2023-06-01 20:17:00 +08:00
|
|
|
|
} else {
|
|
|
|
|
this.toggleInt(false);
|
2023-06-04 16:02:30 +08:00
|
|
|
|
this.noteContext.viewScope.highlightedTextPreviousVisible = false;
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 20:17:00 +08:00
|
|
|
|
this.triggerCommand("reEvaluateRightPaneVisibility");
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
2023-06-01 20:17:00 +08:00
|
|
|
|
|
2023-05-31 18:32:33 +08:00
|
|
|
|
/**
|
2023-06-04 16:02:30 +08:00
|
|
|
|
* Builds a table of helight text.
|
2023-05-31 18:32:33 +08:00
|
|
|
|
*/
|
2023-06-03 11:55:50 +08:00
|
|
|
|
getHlt(html, optionsHlt) {
|
|
|
|
|
// matches a span containing background-color
|
|
|
|
|
const regex1 = /<span[^>]*style\s*=\s*[^>]*background-color:[^>]*?>[\s\S]*?<\/span>/gi;
|
|
|
|
|
// matches a span containing color
|
|
|
|
|
const regex2 = /<span[^>]*style\s*=\s*[^>]*[^-]color:[^>]*?>[\s\S]*?<\/span>/gi;
|
|
|
|
|
// match italics
|
|
|
|
|
const regex3 = /<i>[\s\S]*?<\/i>/gi;
|
|
|
|
|
// match bold
|
|
|
|
|
const regex4 = /<strong>[\s\S]*?<\/strong>/gi;
|
|
|
|
|
// match underline
|
|
|
|
|
const regex5 = /<u>[\s\S]*?<\/u>/g;
|
|
|
|
|
// Possible values in optionsHlt: '["bold","italic","underline","color","bgColor"]'
|
2023-06-04 16:02:30 +08:00
|
|
|
|
// element priority: span>i>strong>u
|
2023-06-03 11:55:50 +08:00
|
|
|
|
let findSubStr="", combinedRegexStr = "";
|
|
|
|
|
if (optionsHlt.indexOf("bgColor") >= 0){
|
|
|
|
|
findSubStr+=`,span[style*="background-color"]`;
|
|
|
|
|
combinedRegexStr+=`|${regex1.source}`;
|
|
|
|
|
}
|
|
|
|
|
if (optionsHlt.indexOf("color") >= 0){
|
|
|
|
|
findSubStr+=`,span[style*="color"]`;
|
|
|
|
|
combinedRegexStr+=`|${regex2.source}`;
|
|
|
|
|
}
|
|
|
|
|
if (optionsHlt.indexOf("italic") >= 0){
|
|
|
|
|
findSubStr+=`,i`;
|
|
|
|
|
combinedRegexStr+=`|${regex3.source}`;
|
|
|
|
|
}
|
|
|
|
|
if (optionsHlt.indexOf("bold") >= 0){
|
|
|
|
|
findSubStr+=`,strong`;
|
|
|
|
|
combinedRegexStr+=`|${regex4.source}`;
|
|
|
|
|
}
|
|
|
|
|
if (optionsHlt.indexOf("underline") >= 0){
|
|
|
|
|
findSubStr+=`,u`;
|
|
|
|
|
combinedRegexStr+=`|${regex5.source}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findSubStr = findSubStr.substring(1)
|
|
|
|
|
combinedRegexStr = `(` + combinedRegexStr.substring(1) + `)`;
|
|
|
|
|
const combinedRegex = new RegExp(combinedRegexStr, 'gi');
|
2023-06-03 14:43:20 +08:00
|
|
|
|
let $hlt = $("<ol>");
|
|
|
|
|
let prevEndIndex = -1, hltLiCount = 0;
|
|
|
|
|
for (let match = null, hltIndex=0; ((match = combinedRegex.exec(html)) !== null); hltIndex++) {
|
2023-06-03 11:55:50 +08:00
|
|
|
|
var subHtml = match[0];
|
2023-06-01 20:17:00 +08:00
|
|
|
|
const startIndex = match.index;
|
2023-06-03 11:55:50 +08:00
|
|
|
|
const endIndex = combinedRegex.lastIndex;
|
|
|
|
|
if (prevEndIndex != -1 && startIndex === prevEndIndex) {
|
2023-06-04 16:02:30 +08:00
|
|
|
|
//If the previous element is connected to this element in HTML, then concatenate them into one.
|
2023-06-03 11:55:50 +08:00
|
|
|
|
$hlt.children().last().append(subHtml);
|
|
|
|
|
} else {
|
2023-06-04 16:02:30 +08:00
|
|
|
|
//hide li if its text content is empty
|
2023-06-03 14:43:20 +08:00
|
|
|
|
if ([...subHtml.matchAll(/(?<=^|>)[^><]+?(?=<|$)/g)].map(matchTmp => matchTmp[0]).join('').trim() != ""){
|
|
|
|
|
var $li = $('<li>');
|
|
|
|
|
$li.html(subHtml);
|
|
|
|
|
$li.on("click", () => this.jumpToHlt(findSubStr,hltIndex));
|
|
|
|
|
$hlt.append($li);
|
|
|
|
|
hltLiCount++;
|
|
|
|
|
}else{
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
2023-06-01 20:17:00 +08:00
|
|
|
|
prevEndIndex = endIndex;
|
|
|
|
|
}
|
2023-05-31 18:32:33 +08:00
|
|
|
|
return {
|
|
|
|
|
$hlt,
|
2023-06-01 20:17:00 +08:00
|
|
|
|
hltLiCount
|
2023-05-31 18:32:33 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2023-06-03 11:55:50 +08:00
|
|
|
|
async jumpToHlt(findSubStr,hltIndex) {
|
2023-05-31 18:32:33 +08:00
|
|
|
|
const isReadOnly = await this.noteContext.isReadOnly();
|
2023-06-03 11:55:50 +08:00
|
|
|
|
let targetElement;
|
2023-05-31 18:32:33 +08:00
|
|
|
|
if (isReadOnly) {
|
|
|
|
|
const $container = await this.noteContext.getContentElement();
|
2023-06-03 11:55:50 +08:00
|
|
|
|
targetElement=$container.find(findSubStr).filter(function() {
|
|
|
|
|
if (findSubStr.indexOf("color")>=0 && findSubStr.indexOf("background-color")<0){
|
|
|
|
|
let color = this.style.color;
|
|
|
|
|
return $(this).prop('tagName')=="SPAN" && color==""?false:true;
|
|
|
|
|
}else{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}).filter(function() {
|
|
|
|
|
return $(this).parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent().parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent().parent().parent(findSubStr).length === 0;
|
|
|
|
|
})
|
2023-05-31 18:32:33 +08:00
|
|
|
|
} else {
|
|
|
|
|
const textEditor = await this.noteContext.getTextEditor();
|
2023-06-03 11:55:50 +08:00
|
|
|
|
targetElement=$(textEditor.editing.view.domRoots.values().next().value).find(findSubStr).filter(function() {
|
|
|
|
|
// When finding span[style*="color"] but not looking for span[style*="background-color"],
|
|
|
|
|
// the background-color error will be regarded as color, so it needs to be filtered
|
|
|
|
|
if (findSubStr.indexOf("color")>=0 && findSubStr.indexOf("background-color")<0){
|
|
|
|
|
let color = this.style.color;
|
|
|
|
|
return $(this).prop('tagName')=="SPAN" && color==""?false:true;
|
|
|
|
|
}else{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}).filter(function() {
|
|
|
|
|
//Need to filter out the child elements of the element that has been found
|
|
|
|
|
return $(this).parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent().parent(findSubStr).length === 0
|
|
|
|
|
&& $(this).parent().parent().parent().parent(findSubStr).length === 0;
|
|
|
|
|
})
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
2023-06-03 11:55:50 +08:00
|
|
|
|
targetElement[hltIndex].scrollIntoView({
|
|
|
|
|
behavior: "smooth", block: "center"
|
|
|
|
|
});
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async closeHltCommand() {
|
2023-05-31 21:47:43 +08:00
|
|
|
|
this.noteContext.viewScope.highlightedTextTemporarilyHidden = true;
|
2023-05-31 18:32:33 +08:00
|
|
|
|
await this.refresh();
|
|
|
|
|
this.triggerCommand('reEvaluateRightPaneVisibility');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async entitiesReloadedEvent({ loadResults }) {
|
|
|
|
|
if (loadResults.isNoteContentReloaded(this.noteId)) {
|
|
|
|
|
await this.refresh();
|
|
|
|
|
} else if (loadResults.getAttributes().find(attr => attr.type === 'label'
|
2023-06-01 20:17:00 +08:00
|
|
|
|
&& (attr.name.toLowerCase().includes('readonly') || attr.name === 'hideHighlightWidget')
|
2023-05-31 18:32:33 +08:00
|
|
|
|
&& attributeService.isAffecting(attr, this.note))) {
|
|
|
|
|
await this.refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CloseHltButton extends OnClickButtonWidget {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.icon("bx-x")
|
2023-06-03 17:00:15 +08:00
|
|
|
|
.title("Close HighlightedTextWidget")
|
2023-05-31 18:32:33 +08:00
|
|
|
|
.titlePlacement("bottom")
|
|
|
|
|
.onClick((widget, e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
widget.triggerCommand("closeHlt");
|
|
|
|
|
})
|
2023-05-31 21:47:43 +08:00
|
|
|
|
.class("icon-action close-highlighted-text");
|
2023-05-31 18:32:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|