2020-05-27 18:21:46 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useRef, forwardRef, useImperativeHandle, useCallback } from 'react';
|
2020-11-07 17:59:37 +02:00
|
|
|
const { themeStyle } = require('@joplin/lib/theme');
|
2020-05-27 18:21:46 +02:00
|
|
|
const Mark = require('mark.js/dist/mark.min.js');
|
2020-11-07 17:59:37 +02:00
|
|
|
const markJsUtils = require('@joplin/lib/markJsUtils');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-11-07 17:59:37 +02:00
|
|
|
const { replaceRegexDiacritics, pregQuote } = require('@joplin/lib/string-utils');
|
2020-09-15 15:01:07 +02:00
|
|
|
const styled = require('styled-components').default;
|
|
|
|
|
|
|
|
const StyledRoot = styled.div`
|
2020-11-12 21:13:28 +02:00
|
|
|
width: ${(props: any) => props.width}px;
|
|
|
|
height: ${(props: any) => props.height}px;
|
|
|
|
opacity: ${(props: any) => props.isProvisional ? '0.5' : '1'};
|
2020-09-15 15:01:07 +02:00
|
|
|
max-width: 100%;
|
|
|
|
box-sizing: border-box;
|
|
|
|
display: flex;
|
|
|
|
align-items: stretch;
|
|
|
|
position: relative;
|
2020-11-12 21:13:28 +02:00
|
|
|
background-color: ${(props: any) => props.selected ? props.theme.selectedColor : 'none'};
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
border-style: solid;
|
2020-11-12 21:13:28 +02:00
|
|
|
border-color: ${(props: any) => props.theme.color};
|
|
|
|
border-top-width: ${(props: any) => props.dragItemPosition === 'top' ? 2 : 0}px;
|
|
|
|
border-bottom-width: ${(props: any) => props.dragItemPosition === 'bottom' ? 2 : 0}px;
|
2020-09-15 15:01:07 +02:00
|
|
|
border-right: none;
|
|
|
|
border-left: none;
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/50174448/css-how-to-add-white-space-before-elements-border
|
|
|
|
&::before {
|
|
|
|
content: '';
|
2020-11-12 21:13:28 +02:00
|
|
|
border-bottom: 1px solid ${(props: any) => props.theme.dividerColor};
|
|
|
|
width: ${(props: any) => props.width - 32}px;
|
2020-09-15 15:01:07 +02:00
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
left: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
2020-11-12 21:13:28 +02:00
|
|
|
background-color: ${(props: any) => props.theme.backgroundColorHover3};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
`;
|
2020-05-27 18:21:46 +02:00
|
|
|
|
|
|
|
interface NoteListItemProps {
|
2020-11-12 21:29:22 +02:00
|
|
|
themeId: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
style: any;
|
|
|
|
dragItemIndex: number;
|
|
|
|
highlightedWords: string[];
|
|
|
|
index: number;
|
|
|
|
isProvisional: boolean;
|
|
|
|
isSelected: boolean;
|
|
|
|
isWatched: boolean;
|
|
|
|
item: any;
|
|
|
|
itemCount: number;
|
|
|
|
onCheckboxClick: any;
|
|
|
|
onDragStart: any;
|
|
|
|
onNoteDragOver: any;
|
|
|
|
onTitleClick: any;
|
|
|
|
onContextMenu(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>): void;
|
2020-05-27 18:21:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function NoteListItem(props: NoteListItemProps, ref: any) {
|
2020-05-27 18:21:46 +02:00
|
|
|
const item = props.item;
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(props.themeId);
|
|
|
|
const hPadding = 16;
|
2020-05-27 18:21:46 +02:00
|
|
|
|
|
|
|
const anchorRef = useRef(null);
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => {
|
|
|
|
return {
|
|
|
|
focus: function() {
|
|
|
|
if (anchorRef.current) anchorRef.current.focus();
|
|
|
|
},
|
2022-06-08 11:33:06 +02:00
|
|
|
getHeight: () => anchorRef.current?.clientHeight,
|
2020-05-27 18:21:46 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
let dragItemPosition = '';
|
2020-05-27 18:21:46 +02:00
|
|
|
if (props.dragItemIndex === props.index) {
|
2020-09-15 15:01:07 +02:00
|
|
|
dragItemPosition = 'top';
|
2020-05-27 18:21:46 +02:00
|
|
|
} else if (props.index === props.itemCount - 1 && props.dragItemIndex >= props.itemCount) {
|
2020-09-15 15:01:07 +02:00
|
|
|
dragItemPosition = 'bottom';
|
2020-05-27 18:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const onTitleClick = useCallback((event) => {
|
|
|
|
props.onTitleClick(event, props.item);
|
|
|
|
}, [props.onTitleClick, props.item]);
|
|
|
|
|
|
|
|
const onCheckboxClick = useCallback((event) => {
|
|
|
|
props.onCheckboxClick(event, props.item);
|
|
|
|
}, [props.onCheckboxClick, props.item]);
|
|
|
|
|
|
|
|
// Setting marginBottom = 1 because it makes the checkbox looks more centered, at least on Windows
|
|
|
|
// but don't know how it will look in other OSes.
|
|
|
|
function renderCheckbox() {
|
|
|
|
if (!item.is_todo) return null;
|
|
|
|
|
|
|
|
return (
|
2020-09-15 15:01:07 +02:00
|
|
|
<div style={{ display: 'flex', height: props.height, alignItems: 'center', paddingLeft: hPadding }}>
|
2020-05-27 18:21:46 +02:00
|
|
|
<input
|
|
|
|
style={{ margin: 0, marginBottom: 1, marginRight: 5 }}
|
|
|
|
type="checkbox"
|
|
|
|
checked={!!item.todo_completed}
|
|
|
|
onChange={onCheckboxClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
let listItemTitleStyle = { ...props.style.listItemTitle };
|
2020-05-27 18:21:46 +02:00
|
|
|
listItemTitleStyle.paddingLeft = !item.is_todo ? hPadding : 4;
|
2022-04-11 18:46:33 +02:00
|
|
|
if (item.is_shared) listItemTitleStyle.color = theme.colorWarn3;
|
2023-06-01 13:02:36 +02:00
|
|
|
if (item.is_todo && !!item.todo_completed) listItemTitleStyle = { ...listItemTitleStyle, ...props.style.listItemTitleCompleted };
|
2020-05-27 18:21:46 +02:00
|
|
|
|
|
|
|
const displayTitle = Note.displayTitle(item);
|
|
|
|
let titleComp = null;
|
|
|
|
|
|
|
|
if (props.highlightedWords.length) {
|
|
|
|
const titleElement = document.createElement('span');
|
|
|
|
titleElement.textContent = displayTitle;
|
|
|
|
const mark = new Mark(titleElement, {
|
|
|
|
exclude: ['img'],
|
|
|
|
acrossElements: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
mark.unmark();
|
|
|
|
|
2023-02-14 17:13:31 +02:00
|
|
|
try {
|
|
|
|
for (const wordToBeHighlighted of props.highlightedWords) {
|
|
|
|
markJsUtils.markKeyword(mark, wordToBeHighlighted, {
|
|
|
|
pregQuote: pregQuote,
|
|
|
|
replaceRegexDiacritics: replaceRegexDiacritics,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name !== 'SyntaxError') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
// An error of 'Regular expression too large' might occour in the markJs library
|
|
|
|
// when the input is really big, this catch is here to avoid the application crashing
|
|
|
|
// https://github.com/laurent22/joplin/issues/7634
|
|
|
|
console.error('Error while trying to highlight words from search: ', error);
|
2020-05-27 18:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: in this case it is safe to use dangerouslySetInnerHTML because titleElement
|
|
|
|
// is a span tag that we created and that contains data that's been inserted as plain text
|
|
|
|
// with `textContent` so it cannot contain any XSS attacks. We use this feature because
|
|
|
|
// mark.js can only deal with DOM elements.
|
|
|
|
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
|
|
|
|
titleComp = <span dangerouslySetInnerHTML={{ __html: titleElement.outerHTML }}></span>;
|
|
|
|
} else {
|
|
|
|
titleComp = <span>{displayTitle}</span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const watchedIconStyle = {
|
|
|
|
paddingRight: 4,
|
|
|
|
color: theme.color,
|
|
|
|
};
|
|
|
|
const watchedIcon = props.isWatched ? null : <i style={watchedIconStyle} className={'fa fa-share-square'}></i>;
|
2021-05-17 20:33:44 +02:00
|
|
|
const classNames = [
|
|
|
|
'list-item-container',
|
|
|
|
props.isSelected && 'selected',
|
|
|
|
item.todo_completed && 'todo-completed',
|
|
|
|
item.is_todo ? 'todo-list-item' : 'note-list-item',
|
|
|
|
(props.index + 1) % 2 === 0 ? 'even' : 'odd',
|
|
|
|
]
|
|
|
|
.filter(e => !!e)
|
|
|
|
.join(' ');
|
2020-05-27 18:21:46 +02:00
|
|
|
|
|
|
|
// Need to include "todo_completed" in key so that checkbox is updated when
|
|
|
|
// item is changed via sync.
|
|
|
|
return (
|
2020-09-15 15:01:07 +02:00
|
|
|
<StyledRoot
|
2021-05-17 20:33:44 +02:00
|
|
|
className={classNames}
|
2020-09-15 15:01:07 +02:00
|
|
|
onDragOver={props.onNoteDragOver}
|
|
|
|
width={props.width}
|
|
|
|
height={props.height}
|
|
|
|
isProvisional={props.isProvisional}
|
|
|
|
selected={props.isSelected}
|
|
|
|
dragItemPosition={dragItemPosition}
|
|
|
|
>
|
2020-05-27 18:21:46 +02:00
|
|
|
{renderCheckbox()}
|
|
|
|
<a
|
|
|
|
ref={anchorRef}
|
2020-06-06 16:47:02 +02:00
|
|
|
onContextMenu={props.onContextMenu}
|
2020-05-27 18:21:46 +02:00
|
|
|
href="#"
|
|
|
|
draggable={true}
|
|
|
|
style={listItemTitleStyle}
|
|
|
|
onClick={onTitleClick}
|
|
|
|
onDragStart={props.onDragStart}
|
|
|
|
data-id={item.id}
|
|
|
|
>
|
|
|
|
{watchedIcon}
|
|
|
|
{titleComp}
|
|
|
|
</a>
|
2020-09-15 15:01:07 +02:00
|
|
|
</StyledRoot>
|
2020-05-27 18:21:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default forwardRef(NoteListItem);
|