mirror of https://github.com/halo-dev/halo
feat: add setting item for primary menu selector (#2667)
parent
8d6838a956
commit
a2bb3d4a00
|
@ -81,4 +81,10 @@ public class SystemSetting {
|
|||
Boolean requireReviewForNew;
|
||||
Boolean systemUserOnly;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Menu {
|
||||
public static final String GROUP = "menu";
|
||||
public String primary;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,5 +12,5 @@ public interface MenuFinder {
|
|||
|
||||
MenuVo getByName(String name);
|
||||
|
||||
MenuVo getDefault();
|
||||
MenuVo getPrimary();
|
||||
}
|
||||
|
|
|
@ -9,11 +9,15 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.comparator.Comparators;
|
||||
import run.halo.app.core.extension.Menu;
|
||||
import run.halo.app.core.extension.MenuItem;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.SystemSetting;
|
||||
import run.halo.app.theme.finders.Finder;
|
||||
import run.halo.app.theme.finders.MenuFinder;
|
||||
import run.halo.app.theme.finders.vo.MenuItemVo;
|
||||
|
@ -26,13 +30,11 @@ import run.halo.app.theme.finders.vo.MenuVo;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Finder("menuFinder")
|
||||
@AllArgsConstructor
|
||||
public class MenuFinderImpl implements MenuFinder {
|
||||
|
||||
private final ReactiveExtensionClient client;
|
||||
|
||||
public MenuFinderImpl(ReactiveExtensionClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
@Override
|
||||
public MenuVo getByName(String name) {
|
||||
|
@ -43,14 +45,19 @@ public class MenuFinderImpl implements MenuFinder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public MenuVo getDefault() {
|
||||
public MenuVo getPrimary() {
|
||||
List<MenuVo> menuVos = listAsTree();
|
||||
if (CollectionUtils.isEmpty(menuVos)) {
|
||||
return null;
|
||||
}
|
||||
// TODO If there are multiple groups of menus,
|
||||
// return the first as the default, and consider optimizing it later
|
||||
return menuVos.get(0);
|
||||
return environmentFetcher.fetch(SystemSetting.Menu.GROUP, SystemSetting.Menu.class)
|
||||
.blockOptional()
|
||||
.map(SystemSetting.Menu::getPrimary)
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.flatMap(primary -> menuVos.stream()
|
||||
.filter(menuVo -> menuVo.getMetadata().getName().equals(primary))
|
||||
.findAny())
|
||||
.orElse(menuVos.get(0));
|
||||
}
|
||||
|
||||
List<MenuVo> listAll() {
|
||||
|
|
|
@ -18,4 +18,8 @@ data:
|
|||
{
|
||||
"globalHead": "",
|
||||
"footer": ""
|
||||
}
|
||||
menu: |
|
||||
{
|
||||
"primary": "primary"
|
||||
}
|
|
@ -7,9 +7,9 @@ import static org.mockito.ArgumentMatchers.eq;
|
|||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
@ -34,13 +34,9 @@ class MenuFinderImplTest {
|
|||
@Mock
|
||||
private ReactiveExtensionClient client;
|
||||
|
||||
@InjectMocks
|
||||
private MenuFinderImpl menuFinder;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
menuFinder = new MenuFinderImpl(client);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAsTree() {
|
||||
Tuple2<List<Menu>, List<MenuItem>> tuple = testTree();
|
||||
|
|
Loading…
Reference in New Issue