2020-10-02 15:10:00 +00:00
|
|
|
import { isDesktop } from 'app/client/lib/browserInfo';
|
2022-10-28 16:11:08 +00:00
|
|
|
import { makeT } from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { cssEditorInput } from "app/client/ui/HomeLeftPane";
|
|
|
|
import { itemHeader, itemHeaderWrapper, treeViewContainer } from "app/client/ui/TreeViewComponentCss";
|
2022-09-06 01:51:57 +00:00
|
|
|
import { theme } from "app/client/ui2018/cssVars";
|
2020-10-02 15:10:00 +00:00
|
|
|
import { icon } from "app/client/ui2018/icons";
|
2023-03-13 07:03:59 +00:00
|
|
|
import { hoverTooltip, overflowTooltip } from 'app/client/ui/tooltips';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { menu, menuItem, menuText } from "app/client/ui2018/menus";
|
2023-06-26 16:58:29 +00:00
|
|
|
import { Computed, dom, domComputed, DomElementArg, makeTestId, observable, Observable, styled } from "grainjs";
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-12-09 15:46:03 +00:00
|
|
|
const t = makeT('pages');
|
2022-10-28 16:11:08 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const testId = makeTestId('test-docpage-');
|
|
|
|
|
|
|
|
// the actions a page can do
|
|
|
|
export interface PageActions {
|
|
|
|
onRename: (name: string) => Promise<void>|any;
|
|
|
|
onRemove: () => void;
|
|
|
|
onDuplicate: () => void;
|
|
|
|
isRemoveDisabled: () => boolean;
|
|
|
|
isReadonly: Observable<boolean>;
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:08:15 +00:00
|
|
|
function isTargetSelected(target: HTMLElement) {
|
|
|
|
const parentItemHeader = target.closest('.' + itemHeader.className);
|
|
|
|
return parentItemHeader ? parentItemHeader.classList.contains('selected') : false;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// build the dom for a document page entry. It shows an icon (for now the first letter of the name,
|
|
|
|
// but later we'll support user selected icon), the name and a dots menu containing a "Rename" and
|
|
|
|
// "Remove" entries. Clicking "Rename" turns the page name into an editable input, which then call
|
|
|
|
// the actions.onRename callback with the new name. Setting actions.onRemove to undefined disables
|
|
|
|
// the item in the menu.
|
2021-07-30 18:05:16 +00:00
|
|
|
export function buildPageDom(name: Observable<string>, actions: PageActions, ...args: DomElementArg[]) {
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
const isRenaming = observable(false);
|
|
|
|
const pageMenu = () => [
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItem(() => isRenaming.set(true), t("Rename"), testId('rename'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', actions.isReadonly)),
|
2022-12-06 13:35:10 +00:00
|
|
|
menuItem(actions.onRemove, t("Remove"), testId('remove'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', (use) => use(actions.isReadonly) || actions.isRemoveDisabled())),
|
2022-12-06 13:35:10 +00:00
|
|
|
menuItem(actions.onDuplicate, t("Duplicate Page"), testId('duplicate'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', actions.isReadonly)),
|
2022-12-06 13:35:10 +00:00
|
|
|
dom.maybe(actions.isReadonly, () => menuText(t("You do not have edit access to this document"))),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
let pageElem: HTMLElement;
|
|
|
|
|
|
|
|
// toggle '-renaming' class on the item's header. This is useful to make the background remain the
|
|
|
|
// same while opening dots menu
|
|
|
|
const lis = isRenaming.addListener(() => {
|
|
|
|
const parent = pageElem.closest('.' + itemHeader.className);
|
|
|
|
if (parent) {
|
|
|
|
dom.clsElem(parent, itemHeader.className + '-renaming', isRenaming.get());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-26 16:58:29 +00:00
|
|
|
const splitName = Computed.create(null, name, (use, _name) => splitPageInitial(_name));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
return pageElem = dom(
|
|
|
|
'div',
|
|
|
|
dom.autoDispose(lis),
|
2023-06-26 16:58:29 +00:00
|
|
|
dom.autoDispose(splitName),
|
2021-03-01 16:51:30 +00:00
|
|
|
domComputed((use) => use(name) === '', blank => blank ? dom('div', '-') :
|
|
|
|
domComputed(isRenaming, (isrenaming) => (
|
|
|
|
isrenaming ?
|
|
|
|
cssPageItem(
|
2022-08-04 13:50:59 +00:00
|
|
|
cssPageInitial(
|
|
|
|
testId('initial'),
|
2023-06-26 16:58:29 +00:00
|
|
|
dom.text((use) => use(splitName).initial),
|
|
|
|
cssPageInitial.cls('-emoji', (use) => use(splitName).hasEmoji),
|
|
|
|
),
|
2021-03-01 16:51:30 +00:00
|
|
|
cssEditorInput(
|
|
|
|
{
|
2021-07-30 18:05:16 +00:00
|
|
|
initialValue: name.get() || '',
|
2021-03-01 16:51:30 +00:00
|
|
|
save: (val) => actions.onRename(val),
|
|
|
|
close: () => isRenaming.set(false)
|
|
|
|
},
|
|
|
|
testId('editor'),
|
|
|
|
dom.on('mousedown', (ev) => ev.stopPropagation()),
|
|
|
|
dom.on('click', (ev) => { ev.stopPropagation(); ev.preventDefault(); })
|
|
|
|
),
|
|
|
|
// Note that we don't pass extra args when renaming is on, because they usually includes
|
2022-02-19 09:46:49 +00:00
|
|
|
// mouse event handlers interfering with input editor and yields wrong behavior on
|
2021-03-01 16:51:30 +00:00
|
|
|
// firefox.
|
|
|
|
) :
|
|
|
|
cssPageItem(
|
2022-08-04 13:50:59 +00:00
|
|
|
cssPageInitial(
|
|
|
|
testId('initial'),
|
2023-06-26 16:58:29 +00:00
|
|
|
dom.text((use) => use(splitName).initial),
|
|
|
|
cssPageInitial.cls('-emoji', (use) => use(splitName).hasEmoji),
|
2022-08-04 13:50:59 +00:00
|
|
|
),
|
2022-08-03 13:08:15 +00:00
|
|
|
cssPageName(
|
2023-06-26 16:58:29 +00:00
|
|
|
dom.text((use) => use(splitName).displayName),
|
2022-08-03 13:08:15 +00:00
|
|
|
testId('label'),
|
|
|
|
dom.on('click', (ev) => isTargetSelected(ev.target as HTMLElement) && isRenaming.set(true)),
|
2023-03-13 07:03:59 +00:00
|
|
|
overflowTooltip(),
|
2022-08-03 13:08:15 +00:00
|
|
|
),
|
2021-03-01 16:51:30 +00:00
|
|
|
cssPageMenuTrigger(
|
2022-09-06 01:51:57 +00:00
|
|
|
cssPageMenuIcon('Dots'),
|
2021-03-01 16:51:30 +00:00
|
|
|
menu(pageMenu, {placement: 'bottom-start', parentSelectorToMark: '.' + itemHeader.className}),
|
|
|
|
dom.on('click', (ev) => { ev.stopPropagation(); ev.preventDefault(); }),
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-03-01 16:51:30 +00:00
|
|
|
// Let's prevent dragging to start when un-intentionally holding the mouse down on '...' menu.
|
|
|
|
dom.on('mousedown', (ev) => ev.stopPropagation()),
|
|
|
|
testId('dots'),
|
|
|
|
),
|
|
|
|
// Prevents the default dragging behaviour that Firefox support for links which conflicts
|
|
|
|
// with our own dragging pages.
|
|
|
|
dom.on('dragstart', (ev) => ev.preventDefault()),
|
|
|
|
args
|
|
|
|
)
|
|
|
|
)),
|
|
|
|
));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:04:59 +00:00
|
|
|
export function buildCensoredPage() {
|
|
|
|
return cssPageItem(
|
|
|
|
cssPageInitial(
|
|
|
|
testId('initial'),
|
|
|
|
dom.text('C'),
|
|
|
|
),
|
|
|
|
cssCensoredPageName(
|
|
|
|
dom.text('CENSORED'),
|
|
|
|
testId('label'),
|
|
|
|
),
|
|
|
|
hoverTooltip('This page is censored due to access rules.'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-26 16:58:29 +00:00
|
|
|
// This crazy expression matches all "possible emoji" and comes from a very official source:
|
|
|
|
// https://unicode.org/reports/tr51/#EBNF_and_Regex (linked from
|
|
|
|
// https://stackoverflow.com/a/68146409/328565). It is processed from the original by replacing \x
|
|
|
|
// with \u, removing whitespace, and factoring out a long subexpression.
|
|
|
|
const emojiPart = /(?:\p{RI}\p{RI}|\p{Emoji}(?:\p{EMod}|\u{FE0F}\u{20E3}?|[\u{E0020}-\u{E007E}]+\u{E007F})?)/u;
|
|
|
|
const pageInitialRegex = new RegExp(`^${emojiPart.source}(?:\\u{200D}${emojiPart.source})*`, "u");
|
|
|
|
|
|
|
|
// Divide up the page name into an "initial" and "displayName", where an emoji initial, if
|
|
|
|
// present, is omitted from the displayName, but a regular character used as the initial is kept.
|
|
|
|
function splitPageInitial(name: string): {initial: string, displayName: string, hasEmoji: boolean} {
|
|
|
|
const m = name.match(pageInitialRegex);
|
|
|
|
if (m) {
|
|
|
|
return {initial: m[0], displayName: name.slice(m[0].length).trim(), hasEmoji: true};
|
|
|
|
} else {
|
|
|
|
return {initial: Array.from(name)[0], displayName: name.trim(), hasEmoji: false};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssPageItem = styled('a', `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
height: 28px;
|
|
|
|
align-items: center;
|
|
|
|
flex-grow: 1;
|
|
|
|
.${treeViewContainer.className}-close & {
|
2023-06-26 16:58:29 +00:00
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
&, &:hover, &:focus {
|
|
|
|
text-decoration: none;
|
|
|
|
outline: none;
|
|
|
|
color: inherit;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssPageInitial = styled('div', `
|
|
|
|
flex-shrink: 0;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.pageInitialsFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
border-radius: 3px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pageInitialsBg};
|
2023-06-26 16:58:29 +00:00
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-right: 8px;
|
2023-06-26 16:58:29 +00:00
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
&-emoji {
|
|
|
|
background-color: ${theme.pageInitialsEmojiBg};
|
|
|
|
box-shadow: 0 0 0 1px var(--grist-theme-left-panel-page-emoji-outline, var(--grist-color-dark-grey));
|
|
|
|
font-size: 15px;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
.${treeViewContainer.className}-close & {
|
|
|
|
margin-right: 0;
|
|
|
|
}
|
|
|
|
.${itemHeader.className}.selected &-emoji {
|
|
|
|
box-shadow: none;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssPageName = styled('div', `
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
2022-08-04 13:50:59 +00:00
|
|
|
flex-grow: 1;
|
2020-10-02 15:10:00 +00:00
|
|
|
.${treeViewContainer.className}-close & {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-10-28 08:04:59 +00:00
|
|
|
const cssCensoredPageName = styled(cssPageName, `
|
|
|
|
color: ${theme.disabledPageFg};
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
function onHoverSupport(yesNo: boolean) {
|
|
|
|
// On desktop, we show page menu button on hover over page link. This isn't usable on mobile,
|
|
|
|
// and interferes with clicks on iOS; so instead we show the button when the page is selected.
|
|
|
|
//
|
|
|
|
// We can achieve the distinction in CSS with
|
|
|
|
// @media (hover: hover) { ... }
|
|
|
|
// @media (hover: none) { ... }
|
|
|
|
//
|
|
|
|
// Except that it interferes with tests, because headless Chrome test on Linux incorrectly
|
|
|
|
// matches (hover: none). To work around it, we assume desktop browsers can always hover,
|
|
|
|
// and use trivial match-all/match-none media queries on desktop browsers.
|
|
|
|
if (isDesktop()) {
|
|
|
|
return yesNo ? 'all' : 'not all';
|
|
|
|
} else {
|
|
|
|
return yesNo ? '(hover: hover)' : '(hover: none)';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssPageMenuTrigger = styled('div', `
|
|
|
|
cursor: default;
|
|
|
|
display: none;
|
|
|
|
margin-right: 4px;
|
|
|
|
margin-left: auto;
|
|
|
|
line-height: 0px;
|
|
|
|
border-radius: 3px;
|
|
|
|
height: 24px;
|
|
|
|
width: 24px;
|
|
|
|
padding: 4px;
|
|
|
|
|
|
|
|
.${treeViewContainer.className}-close & {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
&.weasel-popup-open {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
@media ${onHoverSupport(true)} {
|
|
|
|
.${itemHeaderWrapper.className}-not-dragging:hover & {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media ${onHoverSupport(false)} {
|
|
|
|
.${itemHeaderWrapper.className}-not-dragging > .${itemHeader.className}.selected & {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.${itemHeaderWrapper.className}-not-dragging &:hover, &.weasel-popup-open {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pageOptionsHoverBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
.${itemHeaderWrapper.className}-not-dragging > .${itemHeader.className}.selected &:hover,
|
|
|
|
.${itemHeaderWrapper.className}-not-dragging > .${itemHeader.className}.selected &.weasel-popup-open {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pageOptionsSelectedHoverBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.${itemHeader.className}.weasel-popup-open, .${itemHeader.className}-renaming {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pageHoverBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
const cssPageMenuIcon = styled(icon, `
|
|
|
|
background-color: ${theme.pageOptionsFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
.${itemHeader.className}.selected & {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pageOptionsHoverFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|