Skip to content

Commit

Permalink
Highlight and select teasers from sidebar
Browse files Browse the repository at this point in the history
REDMINE-20852, REDMINE-20896
  • Loading branch information
tf committed Jan 16, 2025
1 parent 4879fcf commit b22741d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ describe('ExternalLinkCollection', () => {
expect(listener).toHaveBeenCalledTimes(1);
});

it('posts content element command on highlight', () => {
const contentElement = factories.contentElement({
id: 10,
configuration: {
links: [
{id: 1},
]
}
});
const itemsCollection = ExternalLinkCollection.forContentElement(contentElement);
const listener = jest.fn();

contentElement.on('postCommand', listener);
itemsCollection.get(1).highlight();

expect(listener).toHaveBeenCalledWith(10, {type: 'HIGHLIGHT_ITEM', index: 0});
});

it('posts content element command on resetHighlight', () => {
const contentElement = factories.contentElement({
id: 10,
configuration: {
links: [
{id: 1},
]
}
});
const itemsCollection = ExternalLinkCollection.forContentElement(contentElement);
const listener = jest.fn();

contentElement.on('postCommand', listener);
itemsCollection.get(1).resetHighlight();

expect(listener).toHaveBeenCalledWith(10, {type: 'RESET_ITEM_HIGHLIGHT'});
});

it('return empty title by default', () => {
const contentElement = factories.contentElement({
id: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,62 @@ describe('ExternalLinkList', () => {
expect(container.querySelector(`.${linkStyles.selected}`)).toBeNull();
expect(setTransientState).toHaveBeenCalledWith({selectedItemId: null})
});

it('supports highlighting item via command', () => {
const configuration = {
links: [
{id: 1}
]
};

const {container, triggerEditorCommand} = renderInContentElement(
<ExternalLinkList configuration={configuration} sectionProps={{}} />,
{
editorState: {isSelected: true, isEditable: true}
}
);
triggerEditorCommand({type: 'HIGHLIGHT_ITEM', index: 0});

expect(container.querySelector(`.${linkStyles.highlighted}`)).not.toBeNull();
});

it('supports resetting item highlight via command', () => {
const configuration = {
links: [
{id: 1}
]
};

const {container, triggerEditorCommand} = renderInContentElement(
<ExternalLinkList configuration={configuration} sectionProps={{}} />,
{
editorState: {isSelected: true, isEditable: true}
}
);

triggerEditorCommand({type: 'HIGHLIGHT_ITEM', index: 0});
triggerEditorCommand({type: 'RESET_ITEM_HIGHLIGHT'});

expect(container.querySelector(`.${linkStyles.highlighted}`)).toBeNull();
});

it('supports setting selected item via command', () => {
const configuration = {
links: [
{id: 1}
]
};

const {container, triggerEditorCommand} = renderInContentElement(
<ExternalLinkList configuration={configuration} sectionProps={{}} />,
{
editorState: {isSelected: true, isEditable: true}
}
);
triggerEditorCommand({type: 'SET_SELECTED_ITEM', index: 0});

expect(container.querySelector(`.${linkStyles.selected}`)).not.toBeNull();
});
});

function value(text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export const SidebarEditLinkView = Marionette.Layout.extend({
this.formContainer.show(configurationEditor);
},
goBack: function() {
this.options.contentElement.postCommand({type: 'SET_SELECTED_ITEM',
index: -1});

editor.navigate(`/scrolled/content_elements/${this.options.contentElement.get('id')}`, {trigger: true});
},
destroyLink: function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const SidebarListView = Marionette.Layout.extend({
this.linksContainer.show(new ListView({
collection: this.collection,
sortable: true,
highlight: true,

onEdit: _.bind(this.onEdit, this),
onRemove: _.bind(this.onRemove, this)
}));
Expand All @@ -40,6 +42,9 @@ export const SidebarListView = Marionette.Layout.extend({
},

onEdit: function (linkModel) {
this.options.contentElement.postCommand({type: 'SET_SELECTED_ITEM',
index: this.collection.indexOf(linkModel)});

editor.navigate(`/scrolled/external_links/${this.options.contentElement.id}/${linkModel.id}`, {trigger: true});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ export const ExternalLinkModel = Backbone.Model.extend({
setFilePositions: function(attribute, x, y) {
this.set('thumbnailCropPosition', {x, y});
},

highlight() {
this.collection.contentElement.postCommand({
type: 'HIGHLIGHT_ITEM',
index: this.collection.indexOf(this)
});
},

resetHighlight() {
this.collection.contentElement.postCommand({
type: 'RESET_ITEM_HIGHLIGHT'
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function ExternalLink({id, configuration, ...props}) {
styles[`textPosition-${props.textPosition}`],
{[styles.link]: !!href},
{[styles.outlined]: props.outlined},
{[styles.highlighted]: props.highlighted},
{[styles.selected]: props.selected})}
onClick={props.onClick}>
<Link isEditable={isEditable}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
opacity: 0;
}

.highlighted::before,
.item:where(:hover)::before {
opacity: 0.5;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useState} from 'react';
import classNames from 'classnames';
import {
useContentElementEditorCommandSubscription,
useContentElementEditorState,
useContentElementLifecycle,
useDarkBackground,
Expand Down Expand Up @@ -28,6 +29,19 @@ export function ExternalLinkList(props) {

const {setTransientState, isSelected} = useContentElementEditorState();
const [selectedItemId, setSelectedItemId] = useState();
const [highlightedIndex, setHighlightedIndex] = useState(-1);

useContentElementEditorCommandSubscription(command => {
if (command.type === 'HIGHLIGHT_ITEM') {
setHighlightedIndex(command.index);
}
else if (command.type === 'RESET_ITEM_HIGHLIGHT') {
setHighlightedIndex(-1);
}
else if (command.type === 'SET_SELECTED_ITEM') {
setSelectedItemId(linkList[command.index]?.id);
}
});

function handleItemClick(event, id) {
if (isSelected) {
Expand Down Expand Up @@ -86,6 +100,7 @@ export function ExternalLinkList(props) {
invert={!darkBackground}
loadImages={shouldLoad}
outlined={isSelected}
highlighted={highlightedIndex === index}
selected={link.id === selectedItemId && isSelected}
onClick={event => handleItemClick(event, link.id)} />
)}
Expand Down

0 comments on commit b22741d

Please sign in to comment.