Skip to content

Commit

Permalink
Add view editor visibility toggle when in "custom" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 19, 2024
1 parent d016d26 commit 41b5098
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added propagation of meaningful `source` view props into nested views rendering (action buttons, prelude, and postlude) through the context as `sourceViewProps`
- Standardized monospace font size and line height in `struct`, `signature` and `source` views to relay on root settings
- Added suggestions toggle in query editor on "discovery" page
- Added view editor visibility toggle when in "custom" mode on "discovery" page
- Added `Cmd+Click` or `Ctrl+Click` in inspect mode to open hovered view page in view's showcase
- Added `computeClassName()` and `applyComputedClassName()` methods to view render context API
- Added root CSS properties: `--discovery-monospace-font-size` and `--discovery-monospace-line-height`
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function createElement<TagName extends keyof HTMLElementTagNameMap>(
tag: TagName,
attrs?: CreateElementAttrs<TagName> | string | null,
children?: (Node | string)[] | string
) {
): HTMLElementTagNameMap[TagName] {
const el = document.createElement(tag);

if (typeof attrs === 'string') {
Expand Down
7 changes: 0 additions & 7 deletions src/pages/discovery/editor-common.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
cursor: pointer;
}
.page-discovery > .discovery-editor .discovery-editor-tab.active {
padding-bottom: 3px;
margin-bottom: 0;
background: rgba(108, 188, 241, 0.15);
cursor: default;
Expand All @@ -36,16 +35,10 @@
}
.page-discovery > .discovery-editor .discovery-editor-tab:first-child {
border-top-left-radius: 4px;
}
.page-discovery > .discovery-editor .discovery-editor-tab.active:first-child {
border-bottom-left-radius: 4px;
padding-bottom: 2px;
margin-bottom: 1px;
}
.page-discovery > .discovery-editor .discovery-editor-tab:last-child {
border-top-right-radius: 4px;
}
.page-discovery > .discovery-editor .discovery-editor-tab:not(.active):last-child {
border-bottom-right-radius: 4px;
}

Expand Down
41 changes: 41 additions & 0 deletions src/pages/discovery/editor-view.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@
display: inline-block;
content: 'View:';
}
.page-discovery > .discovery-editor .discovery-editor-tab[data-mode="default"] {
border-top-left-radius: 4px;
}
.page-discovery > .discovery-editor .view-editor-form:not(.hide-editor) .discovery-editor-tab[data-mode="default"] {
border-bottom-left-radius: 0;
}
.page-discovery > .discovery-editor .discovery-editor-tab[data-mode="custom"] {
cursor: pointer;
}
.page-discovery > .discovery-editor .view-editor-form:not(.hide-editor) .discovery-editor-tab[data-mode="custom"] {
border-bottom-right-radius: 0;
padding-bottom: 3px;
}
.page-discovery > .discovery-editor .discovery-editor-tabs .discovery-editor-tab:not(.active) .show-view-editor-toggle {
pointer-events: none;
}
.page-discovery > .discovery-editor .discovery-editor-tabs .show-view-editor-toggle {
display: inline-block;
vertical-align: middle;
margin: -3px -9px -7px 1px;
cursor: pointer;
}
.page-discovery > .discovery-editor .discovery-editor-tabs .show-view-editor-toggle::before {
content: '';
display: inline-block;
width: 20px;
aspect-ratio: 1;
mask: url('./img/minus.svg') center no-repeat;
mask-size: 12px;
background-color: light-dark(#aaa, #aaac);
}
.page-discovery > .discovery-editor .discovery-editor-tabs .discovery-editor-tab.active:hover .show-view-editor-toggle::before {
background-color: white;
background-color: light-dark(rgba(0, 141, 255, 1), #ccc);
}
.page-discovery > .discovery-editor .view-editor-form.hide-editor .discovery-editor-tabs .show-view-editor-toggle::before {
mask-image: url('./img/plus.svg')
}
.page-discovery > .discovery-editor .discovery-editor-tabs.presets {
margin-left: 3ex;
}
Expand All @@ -32,6 +70,9 @@
border-bottom-left-radius: 4px;
}

.page-discovery > .discovery-editor .view-editor-form.hide-editor .view-editor-form-content {
display: none;
}
.page-discovery > .discovery-editor .view-editor-form-content {
padding:
8px
Expand Down
29 changes: 19 additions & 10 deletions src/pages/discovery/editor-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default function(host: ViewModel, updateParams: UpdateHostParams) {
view: string | undefined;
}> = {};

let viewSetupEl: HTMLElement;
let availableViewsEl: HTMLElement;
let availableViewsTextEl: HTMLElement;
let availableViewsListEl: HTMLElement;
Expand All @@ -72,20 +71,29 @@ export default function(host: ViewModel, updateParams: UpdateHostParams) {
createElement('div', {
class: 'discovery-editor-tab',
'data-mode': viewMode.toLowerCase(),
onclick: () => updateParams({
view: viewMode === 'Default' ? undefined : defaultViewSource
}, true)
}, viewMode)
onclick() {
if (!this.classList.contains('active')) {
updateParams({
view: viewMode === 'Default' ? undefined : defaultViewSource,
viewEditorHidden: false
}, true);
} else {
updateParams({
viewEditorHidden: !viewEditorFormEl.classList.contains('hide-editor')
}, true);
}
}
}, viewMode === 'Custom'
? [viewMode, createElement('span', 'show-view-editor-toggle')]
: viewMode
)
)),
/* availablePresetListEl = */createElement('div', 'discovery-editor-tabs presets', viewPresets.map(({ name, content }) =>
createPresetTab(name, content, updateParams)
)),
createElement('div', 'view-editor-form-header-links', '<a href="#views-showcase" class="view-link">Views showcase</a>')
]),
viewSetupEl = createElement('div', {
class: 'view-editor-form-content',
hidden: true
}, [
createElement('div', 'view-editor-form-content', [
createElement('button', {
class: 'view-button formatting',
title: 'Prettify (input should be a JSON)',
Expand Down Expand Up @@ -182,14 +190,15 @@ export default function(host: ViewModel, updateParams: UpdateHostParams) {
const viewContext = contextWithoutEditorParams(context, lastView.context);
const params = getParamsFromContext(context);
const viewMode = typeof params.view === 'string' ? 'custom' : 'default';
const viewEditorHidden = viewMode === 'default' || params.viewEditorHidden === true;
let pageView = params.view;
let view = null;

// update editor
viewEditor.setValue(pageView);

// update view form
viewSetupEl.hidden = viewMode !== 'custom';
viewEditorFormEl.classList.toggle('hide-editor', viewEditorHidden);
viewModeTabsEls.forEach(el =>
el.classList.toggle('active', el.dataset.mode === viewMode)
);
Expand Down
4 changes: 4 additions & 0 deletions src/pages/discovery/img/minus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/pages/discovery/img/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions src/pages/discovery/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ function ensureString(value: unknown, fallback = '') {
return typeof value === 'string' ? value : fallback;
}

export const decodedSpecialParams: (keyof KnownParams)[] = ['query', 'graph', 'view', 'title', 'dzen', 'noedit'] as const;
export const encodedSpecialParams = ['q', 'graph', 'v', 'title', 'dzen', 'noedit'];
export const decodedSpecialParams: (keyof KnownParams)[] = ['query', 'graph', 'view', 'viewEditorHidden', 'title', 'dzen', 'noedit'] as const;
export const encodedSpecialParams = ['q', 'graph', 'v', 'vh', 'title', 'dzen', 'noedit'];

export function encodeParams(params: Partial<Params> | string) {
const normalizedParams = typeof params === 'string' ? { query: params } : params;
const { query, graph, view, title, dzen, noedit, ...extra } = normalizedParams;
const { query, graph, view, viewEditorHidden, title, dzen, noedit, ...extra } = normalizedParams;
const pairs: [name: string, value?: string][] = [];

if (dzen) {
Expand All @@ -37,6 +37,10 @@ export function encodeParams(params: Partial<Params> | string) {
pairs.push(view ? ['v', base64.encode(view)] : ['v']);
}

if (viewEditorHidden) {
pairs.push(['vh']);
}

for (const [name, value] of Object.keys(extra || {}).sort()) {
if (!decodedSpecialParams.includes(name as keyof KnownParams)) {
pairs.push([name, name.endsWith('-b64') && typeof value === 'string'
Expand All @@ -56,6 +60,7 @@ export function decodeParams(pairs: [name: string, value: unknown][]): Params {
query: base64.decode(ensureString(params.q)),
graph: JSON.parse(base64.decode(ensureString(params.graph)) || 'null'),
view: 'v' in params ? base64.decode(ensureString(params.v)) : undefined,
viewEditorHidden: 'vh' in params,
dzen: 'dzen' in params,
noedit: 'noedit' in params
};
Expand Down
1 change: 1 addition & 0 deletions src/pages/discovery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type KnownParams = {
query: string;
graph: Graph;
view: string | undefined;
viewEditorHidden: boolean;
};
export type Params = KnownParams & {
[key: string]: unknown;
Expand Down

0 comments on commit 41b5098

Please sign in to comment.