feat: add app.getFullUrl() method (#6019)

* feat: add app.getFullUrl() method

* fix: test error

* fix: test only
This commit is contained in:
chenos 2025-01-08 20:43:07 +08:00 committed by GitHub
parent 5c97e7c3f9
commit 1d2b237e73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 2 deletions

View File

@ -208,6 +208,14 @@ export class Application {
return this.getPublicPath() + pathname.replace(/^\//g, ''); return this.getPublicPath() + pathname.replace(/^\//g, '');
} }
getFullUrl(pathname: string) {
const name = this.name;
if (name && name !== 'main') {
return this.getPublicPath() + 'apps/' + name + '/' + pathname.replace(/^\//g, '');
}
return this.getPublicPath() + pathname.replace(/^\//g, '');
}
getCollectionManager(dataSource?: string) { getCollectionManager(dataSource?: string) {
return this.dataSourceManager.getDataSource(dataSource)?.collectionManager; return this.dataSourceManager.getDataSource(dataSource)?.collectionManager;
} }

View File

@ -13,11 +13,11 @@ import MockAdapter from 'axios-mock-adapter';
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Link, Outlet } from 'react-router-dom'; import { Link, Outlet } from 'react-router-dom';
import { describe } from 'vitest'; import { describe } from 'vitest';
import { CollectionFieldInterface } from '../../data-source';
import { OpenModeProvider } from '../../modules/popup/OpenModeProvider'; import { OpenModeProvider } from '../../modules/popup/OpenModeProvider';
import { Application } from '../Application'; import { Application } from '../Application';
import { Plugin } from '../Plugin'; import { Plugin } from '../Plugin';
import { useApp } from '../hooks'; import { useApp } from '../hooks';
import { CollectionFieldInterface } from '../../data-source';
describe('Application', () => { describe('Application', () => {
beforeAll(() => { beforeAll(() => {
@ -85,6 +85,32 @@ describe('Application', () => {
}); });
}); });
describe('getFullUrl', () => {
it('default', () => {
const app = new Application({});
expect(app.getFullUrl('test')).toBe('/test');
expect(app.getFullUrl('/test')).toBe('/test');
});
it('custom', () => {
const app = new Application({ publicPath: '/nocobase' });
expect(app.getFullUrl('/test')).toBe('/nocobase/test');
expect(app.getFullUrl('test')).toBe('/nocobase/test');
});
it('sub app', () => {
const app = new Application({ name: 'sub1' });
expect(app.getFullUrl('test')).toBe('/apps/sub1/test');
expect(app.getFullUrl('/test')).toBe('/apps/sub1/test');
});
it('sub app', () => {
const app = new Application({ name: 'sub1', publicPath: '/nocobase/' });
expect(app.getFullUrl('test')).toBe('/nocobase/apps/sub1/test');
expect(app.getFullUrl('/test')).toBe('/nocobase/apps/sub1/test');
});
});
describe('publicPath', () => { describe('publicPath', () => {
it('default', () => { it('default', () => {
const app = new Application({}); const app = new Application({});

View File

@ -95,7 +95,7 @@ export const DesktopModeHeader: FC = () => {
} }
`} `}
> >
<Button style={{ color: 'white' }} href={app.getRouteUrl('/admin')}> <Button style={{ color: 'white' }} href={app.getFullUrl('/admin')}>
{t('Back')} {t('Back')}
</Button> </Button>
<div style={{ display: 'flex', alignItems: 'center' }}> <div style={{ display: 'flex', alignItems: 'center' }}>