diff --git a/extension.json b/extension.json index 7d87360..6b771ab 100644 --- a/extension.json +++ b/extension.json @@ -34,7 +34,8 @@ "ext.aggrid": { "packageFiles": [ "ext.aggrid/init.js", - "ext.aggrid/mountGrid.js" + "ext.aggrid/mountGrid.js", + "ext.aggrid/theme.js" ], "styles": [ "ext.aggrid/ext.aggrid.less" diff --git a/modules/ext.aggrid/mountGrid.js b/modules/ext.aggrid/mountGrid.js index 85d3be4..0f26047 100644 --- a/modules/ext.aggrid/mountGrid.js +++ b/modules/ext.aggrid/mountGrid.js @@ -1,5 +1,7 @@ /* global agGrid */ +const { getWikiTheme } = require( './theme.js' ); + const PLACEHOLDER_SELECTOR = '.ext-aggrid'; const CONFIG_ATTR = 'data-mw-aggrid-options'; // Marks a placeholder as already mounted so re-runs (e.g. wikipage.content @@ -38,6 +40,10 @@ function mountGrid( el ) { if ( !gridOptions ) { return; } + // Apply the wiki theme unless the author already chose one. + if ( !gridOptions.theme ) { + gridOptions.theme = getWikiTheme(); + } // Mark before mounting so a concurrent/re-entrant pass can't double-mount. el.classList.add( INIT_CLASS ); // agGrid is the global exposed by the vendored AG Grid bundle. diff --git a/modules/ext.aggrid/theme.js b/modules/ext.aggrid/theme.js new file mode 100644 index 0000000..550288c --- /dev/null +++ b/modules/ext.aggrid/theme.js @@ -0,0 +1,40 @@ +/* global agGrid */ + +// AG Grid theme colour params mapped to Codex design tokens (CSS variables). +// The grid follows the wiki light/dark/OS scheme and skin colour +// customisations through the cascade — no JS theme-syncing. Fallbacks are the +// REL1_43 light token values, so skins without Codex tokens still render a +// usable light grid. Typography inherits the page font; AG Grid's concrete +// fontSize/spacing defaults are kept (it needs them for row-height layout). +const THEME_PARAMS = { + backgroundColor: 'var(--background-color-base, #fff)', + foregroundColor: 'var(--color-base, #202122)', + borderColor: 'var(--border-color-base, #a2a9b1)', + chromeBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)', + headerBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)', + headerTextColor: 'var(--color-base, #202122)', + accentColor: 'var(--color-progressive, #36c)', + rowHoverColor: 'var(--background-color-interactive-subtle, #f8f9fa)', + selectedRowBackgroundColor: 'var(--background-color-progressive-subtle, #f1f4fd)', + fontFamily: 'inherit', + // Inherit the page color-scheme rather than AG Grid's default of "light", so + // the wrapper doesn't pin itself to light and light-dark() color tokens + // resolve to the page's current mode inside the grid. + browserColorScheme: 'inherit' +}; + +let cachedTheme = null; + +/** + * The shared AG Grid theme, built once from themeQuartz. + * + * @return {Object} An AG Grid Theme object. + */ +function getWikiTheme() { + if ( !cachedTheme ) { + cachedTheme = agGrid.themeQuartz.withParams( THEME_PARAMS ); + } + return cachedTheme; +} + +module.exports = { THEME_PARAMS: THEME_PARAMS, getWikiTheme: getWikiTheme }; diff --git a/tests/vitest/ext.aggrid/mountGrid.test.js b/tests/vitest/ext.aggrid/mountGrid.test.js index 06979bb..0c8f753 100644 --- a/tests/vitest/ext.aggrid/mountGrid.test.js +++ b/tests/vitest/ext.aggrid/mountGrid.test.js @@ -30,7 +30,10 @@ describe( 'parseConfig', () => { describe( 'mountGrid', () => { beforeEach( () => { - global.agGrid = { createGrid: vi.fn() }; + global.agGrid = { + createGrid: vi.fn(), + themeQuartz: { withParams: () => ( { __wiki: true } ) } + }; } ); afterEach( () => { @@ -57,4 +60,18 @@ describe( 'mountGrid', () => { expect( global.agGrid.createGrid ).not.toHaveBeenCalled(); expect( el.classList.contains( 'ext-aggrid--init' ) ).toBe( false ); } ); + + it( 'applies the wiki theme when the config has none', () => { + const el = makeEl( '{"columnDefs":[],"rowData":[]}' ); + mountGrid( el ); + const opts = global.agGrid.createGrid.mock.calls[ 0 ][ 1 ]; + expect( opts.theme ).toEqual( { __wiki: true } ); + } ); + + it( 'preserves a theme already set in the config', () => { + const el = makeEl( '{"columnDefs":[],"rowData":[],"theme":"legacy"}' ); + mountGrid( el ); + const opts = global.agGrid.createGrid.mock.calls[ 0 ][ 1 ]; + expect( opts.theme ).toBe( 'legacy' ); + } ); } ); diff --git a/tests/vitest/ext.aggrid/theme.test.js b/tests/vitest/ext.aggrid/theme.test.js new file mode 100644 index 0000000..ccc8a86 --- /dev/null +++ b/tests/vitest/ext.aggrid/theme.test.js @@ -0,0 +1,45 @@ +const { THEME_PARAMS, getWikiTheme } = require( '../../../modules/ext.aggrid/theme.js' ); + +describe( 'THEME_PARAMS', () => { + const expected = { + backgroundColor: 'var(--background-color-base, #fff)', + foregroundColor: 'var(--color-base, #202122)', + borderColor: 'var(--border-color-base, #a2a9b1)', + chromeBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)', + headerBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)', + headerTextColor: 'var(--color-base, #202122)', + accentColor: 'var(--color-progressive, #36c)', + rowHoverColor: 'var(--background-color-interactive-subtle, #f8f9fa)', + selectedRowBackgroundColor: 'var(--background-color-progressive-subtle, #f1f4fd)' + }; + + it( 'maps every colour param to a Codex token with a fallback', () => { + Object.keys( expected ).forEach( ( key ) => { + expect( THEME_PARAMS[ key ] ).toBe( expected[ key ] ); + } ); + } ); + + it( 'inherits typography rather than setting a font', () => { + expect( THEME_PARAMS.fontFamily ).toBe( 'inherit' ); + expect( THEME_PARAMS.fontSize ).toBeUndefined(); + } ); + + it( 'inherits the browser color-scheme so light-dark() tokens resolve', () => { + expect( THEME_PARAMS.browserColorScheme ).toBe( 'inherit' ); + } ); +} ); + +describe( 'getWikiTheme', () => { + it( 'builds the theme from themeQuartz.withParams and memoises it', () => { + const built = { id: 'wiki-theme' }; + const withParams = vi.fn( () => built ); + global.agGrid = { themeQuartz: { withParams } }; + + expect( getWikiTheme() ).toBe( built ); + expect( getWikiTheme() ).toBe( built ); + expect( withParams ).toHaveBeenCalledTimes( 1 ); + expect( withParams ).toHaveBeenCalledWith( THEME_PARAMS ); + + delete global.agGrid; + } ); +} );