2020-10-02 15:10:00 +00:00
|
|
|
import {docUrl, urlState} from 'app/client/models/gristUrlState';
|
|
|
|
import {getTimeFromNow, HomeModel} from 'app/client/models/HomeModel';
|
2021-06-14 16:54:47 +00:00
|
|
|
import {makeDocOptionsMenu, makeRemovedDocOptionsMenu} from 'app/client/ui/DocMenu';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {transientInput} from 'app/client/ui/transientInput';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {colors, theme, vars} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
|
|
|
import {menu} from 'app/client/ui2018/menus';
|
|
|
|
import * as roles from 'app/common/roles';
|
2021-06-14 16:54:47 +00:00
|
|
|
import {Document, Workspace} from 'app/common/UserAPI';
|
2021-07-28 19:02:06 +00:00
|
|
|
import {computed, dom, makeTestId, Observable, observable, styled} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
const testId = makeTestId('test-dm-');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PinnedDocs builds the dom at the top of the doclist showing all the pinned docs in the
|
|
|
|
* selectedOrg. Builds nothing if there are no pinned docs.
|
|
|
|
*
|
|
|
|
* Used only by DocMenu.
|
|
|
|
*/
|
2021-07-28 19:02:06 +00:00
|
|
|
export function createPinnedDocs(home: HomeModel, docs: Observable<Document[]>, isExample = false) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return pinnedDocList(
|
2021-07-28 19:02:06 +00:00
|
|
|
dom.forEach(docs, doc => buildPinnedDoc(home, doc, doc.workspace, isExample)),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('pinned-doc-list'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a single doc card with a preview and name. A misnomer because it's now used not only for
|
|
|
|
* pinned docs, but also for the thumnbails (aka "icons") view mode.
|
|
|
|
*/
|
2021-07-28 19:02:06 +00:00
|
|
|
export function buildPinnedDoc(home: HomeModel, doc: Document, workspace: Workspace, isExample = false): HTMLElement {
|
2020-10-02 15:10:00 +00:00
|
|
|
const renaming = observable<Document|null>(null);
|
2021-06-01 02:48:32 +00:00
|
|
|
const isRenamingDoc = computed((use) => use(renaming) === doc);
|
2020-10-02 15:10:00 +00:00
|
|
|
return pinnedDocWrapper(
|
2021-06-01 02:48:32 +00:00
|
|
|
dom.autoDispose(isRenamingDoc),
|
2021-06-14 16:54:47 +00:00
|
|
|
dom.domComputed(isRenamingDoc, (isRenaming) =>
|
2021-06-01 02:48:32 +00:00
|
|
|
pinnedDoc(
|
2021-07-28 19:02:06 +00:00
|
|
|
isRenaming || doc.removedAt ?
|
|
|
|
null :
|
2024-02-21 19:22:01 +00:00
|
|
|
urlState().setLinkUrl({...docUrl(doc), ...(isExample ? {org: workspace.orgDomain} : {})}),
|
2021-06-01 02:48:32 +00:00
|
|
|
pinnedDoc.cls('-no-access', !roles.canView(doc.access)),
|
|
|
|
pinnedDocPreview(
|
2021-07-28 19:02:06 +00:00
|
|
|
(doc.options?.icon ?
|
|
|
|
cssImage({src: doc.options.icon}) :
|
|
|
|
[docInitials(doc.name), pinnedDocThumbnail()]
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-07-28 19:02:06 +00:00
|
|
|
(doc.public && !isExample ? cssPublicIcon('PublicFilled', testId('public')) : null),
|
|
|
|
pinnedDocPreview.cls('-with-icon', Boolean(doc.options?.icon)),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-06-01 02:48:32 +00:00
|
|
|
pinnedDocFooter(
|
|
|
|
(isRenaming ?
|
|
|
|
pinnedDocEditorInput({
|
|
|
|
initialValue: doc.name || '',
|
|
|
|
save: async (val) => (val !== doc.name) ? home.renameDoc(doc.id, val) : undefined,
|
|
|
|
close: () => renaming.set(null),
|
|
|
|
}, testId('doc-name-editor'))
|
|
|
|
:
|
|
|
|
pinnedDocTitle(
|
2021-07-28 19:02:06 +00:00
|
|
|
dom.text(doc.name),
|
2021-06-01 02:48:32 +00:00
|
|
|
testId('pinned-doc-name'),
|
|
|
|
// Mostly for the sake of tests, allow .test-dm-pinned-doc-name to find documents in
|
|
|
|
// either 'list' or 'icons' views.
|
|
|
|
testId('doc-name')
|
|
|
|
)
|
|
|
|
),
|
2021-07-28 19:02:06 +00:00
|
|
|
doc.options?.description ?
|
|
|
|
cssPinnedDocDesc(doc.options.description, testId('pinned-doc-desc')) :
|
|
|
|
cssPinnedDocTimestamp(
|
|
|
|
capitalizeFirst(getTimeFromNow(doc.removedAt || doc.updatedAt)),
|
|
|
|
testId('pinned-doc-desc')
|
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
2021-07-28 19:02:06 +00:00
|
|
|
isExample ? null : (doc.removedAt ?
|
2021-06-14 16:54:47 +00:00
|
|
|
[
|
|
|
|
// For deleted documents, attach the menu to the entire doc icon, and include the
|
|
|
|
// "Dots" icon just to clarify that there are options.
|
|
|
|
menu(() => makeRemovedDocOptionsMenu(home, doc, workspace),
|
|
|
|
{placement: 'right-start'}),
|
|
|
|
pinnedDocOptions(icon('Dots'), testId('pinned-doc-options')),
|
|
|
|
] :
|
|
|
|
pinnedDocOptions(icon('Dots'),
|
|
|
|
menu(() => makeDocOptionsMenu(home, doc, renaming),
|
|
|
|
{placement: 'bottom-start'}),
|
|
|
|
// Clicks on the menu trigger shouldn't follow the link that it's contained in.
|
|
|
|
dom.on('click', (ev) => { ev.stopPropagation(); ev.preventDefault(); }),
|
|
|
|
testId('pinned-doc-options'),
|
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
testId('pinned-doc')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function docInitials(docTitle: string) {
|
|
|
|
return cssDocInitials(docTitle.slice(0, 2), testId('pinned-initials'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capitalizes the first letter in the given string.
|
|
|
|
function capitalizeFirst(str: string): string {
|
|
|
|
return str.replace(/^[a-z]/gi, c => c.toUpperCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
const pinnedDocList = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
overflow-x: auto;
|
|
|
|
overflow-y: hidden;
|
|
|
|
padding-bottom: 16px;
|
|
|
|
margin: 0 0 28px 0;
|
|
|
|
`);
|
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
const pinnedDocWrapper = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
display: inline-block;
|
|
|
|
flex: 0 0 auto;
|
|
|
|
position: relative;
|
|
|
|
width: 210px;
|
|
|
|
margin: 16px 24px 16px 0;
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.pinnedDocBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
border-radius: 1px;
|
|
|
|
vertical-align: top;
|
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.pinnedDocBorderHover};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2022-06-17 03:43:26 +00:00
|
|
|
|
|
|
|
/* TODO: Specify a gap on flexbox parents of pinnedDocWrapper instead. */
|
|
|
|
&:last-child {
|
|
|
|
margin-right: 0px;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDoc = styled('a', `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
width: 100%;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
text-decoration: none;
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
&-no-access, &-no-access:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.disabledText};
|
2020-10-02 15:10:00 +00:00
|
|
|
cursor: not-allowed;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDocPreview = styled('div', `
|
|
|
|
position: relative;
|
|
|
|
flex: none;
|
|
|
|
width: 100%;
|
|
|
|
height: 131px;
|
|
|
|
background-color: ${colors.dark};
|
|
|
|
min-height: 0;
|
|
|
|
|
|
|
|
padding: 10px;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
|
|
|
|
.${pinnedDoc.className}-no-access > & {
|
|
|
|
opacity: 0.8;
|
|
|
|
}
|
2021-07-28 19:02:06 +00:00
|
|
|
|
|
|
|
&-with-icon {
|
|
|
|
padding: 0;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDocThumbnail = styled('div', `
|
|
|
|
position: absolute;
|
|
|
|
right: 20px;
|
|
|
|
bottom: 20px;
|
|
|
|
height: 48px;
|
|
|
|
width: 48px;
|
|
|
|
background-image: var(--icon-ThumbPreview);
|
|
|
|
background-size: 48px 48px;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
background-position: center;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssDocInitials = styled('div', `
|
|
|
|
position: absolute;
|
|
|
|
left: 20px;
|
|
|
|
bottom: 20px;
|
|
|
|
font-size: 32px;
|
|
|
|
border: 1px solid ${colors.lightGreen};
|
|
|
|
color: ${colors.mediumGreyOpaque};
|
|
|
|
border-radius: 3px;
|
|
|
|
padding: 4px 0;
|
|
|
|
width: 48px;
|
|
|
|
height: 48px;
|
|
|
|
text-align: center;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDocOptions = styled('div', `
|
|
|
|
position: absolute;
|
|
|
|
top: 12px;
|
|
|
|
right: 12px;
|
|
|
|
height: 24px;
|
|
|
|
width: 24px;
|
|
|
|
padding: 4px;
|
|
|
|
line-height: 0px;
|
|
|
|
border-radius: 3px;
|
|
|
|
cursor: default;
|
|
|
|
visibility: hidden;
|
|
|
|
background-color: ${colors.mediumGrey};
|
|
|
|
--icon-color: ${colors.light};
|
|
|
|
|
|
|
|
.${pinnedDocWrapper.className}:hover &, &.weasel-popup-open {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDocFooter = styled('div', `
|
|
|
|
width: 100%;
|
|
|
|
font-size: ${vars.mediumFontSize};
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.pinnedDocFooterBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const pinnedDocTitle = styled('div', `
|
|
|
|
margin: 16px 16px 0px 16px;
|
|
|
|
font-weight: bold;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
`);
|
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
const pinnedDocEditorInput = styled(transientInput, `
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: 16px 16px 0px 16px;
|
|
|
|
font-weight: bold;
|
|
|
|
min-width: 0px;
|
|
|
|
color: initial;
|
|
|
|
font-size: inherit;
|
|
|
|
line-height: inherit;
|
|
|
|
appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
padding: 0;
|
|
|
|
border: none;
|
|
|
|
outline: none;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
|
|
|
background-color: ${theme.pinnedDocEditorBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
const cssPinnedDocTimestamp = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: 8px 16px 16px 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
const cssPinnedDocDesc = styled(cssPinnedDocTimestamp, `
|
|
|
|
margin: 8px 16px 16px 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2021-07-28 19:02:06 +00:00
|
|
|
height: 48px;
|
2021-07-29 04:24:00 +00:00
|
|
|
line-height: 16px;
|
2021-07-28 19:02:06 +00:00
|
|
|
-webkit-box-orient: vertical;
|
|
|
|
display: -webkit-box;
|
|
|
|
-webkit-line-clamp: 3;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
word-break: break-word;
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssImage = styled('img', `
|
|
|
|
position: relative;
|
2023-09-21 16:57:58 +00:00
|
|
|
background-color: ${colors.dark};
|
2021-07-28 19:02:06 +00:00
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
object-fit: scale-down;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssPublicIcon = styled(icon, `
|
|
|
|
position: absolute;
|
|
|
|
top: 16px;
|
|
|
|
left: 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.accentIcon};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|