Skip to content

Commit

Permalink
fix: update plugin name
Browse files Browse the repository at this point in the history
use static pluginName not the class name to avoid preferences
being saved with minified ClassName keys see issue #238 for more details
  • Loading branch information
joelamb committed Sep 27, 2023
1 parent a34e581 commit 9f3d290
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { BasePlugin, meta, options, preferences } from 'ember-headless-table/plugins';
import { expectTypeOf } from 'expect-type';

Expand Down Expand Up @@ -39,7 +40,7 @@ interface SignatureA {
}

class SimplePlugin<Signature = SignatureA> extends BasePlugin<Signature> {
name = 'my-test-plugin';
pluginName = 'my-test-plugin';
}

// Options
Expand Down
3 changes: 2 additions & 1 deletion ember-headless-table/src/-private/interfaces/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DataTypeOf<T> = T extends Table<infer DataType> ? DataType : T;
*/
export type PluginClass<PluginType> = PluginType & {
new: (...args: unknown[]) => PluginType;
pluginName?: string;
features?: string[];
requires?: string[];
};
Expand Down Expand Up @@ -85,7 +86,7 @@ export interface Plugin<Signature = unknown> {
* - only one plugin of the same name is allowed
* - the name is used for storing preferences / serializable information
*/
name: string;
pluginName?: string;

/**
* Some plugins may require that other plugins be present.
Expand Down
105 changes: 59 additions & 46 deletions ember-headless-table/src/plugins/-private/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ColumnOptionsFor,
OptionsFor,
Plugin,
PluginClass,
RowMetaFor,
TableMetaFor,
} from '#interfaces';
Expand Down Expand Up @@ -121,7 +122,7 @@ export abstract class BasePlugin<Signature = unknown> implements Plugin<Signatur
row?: Constructor<RowMetaFor<Signature>>;
};

abstract name: string;
static pluginName: string;
static features?: string[];
static requires?: string[];
}
Expand Down Expand Up @@ -151,17 +152,17 @@ export const preferences = {
* (though, if other plugins can guess how the underlying plugin access
* works, they can access this data, too. No security guaranteed)
*/
forColumn<P extends BasePlugin<any>, Data = unknown>(column: Column<Data>, klass: Class<P>) {
forColumn<Data = unknown>(column: Column<Data>, klass: PluginClass<any>) {
return {
/**
* delete an entry on the underlying `Map` used for this column-plugin pair
*/
delete(key: string) {
let prefs = column.table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let columnPrefs = existing.forColumn(column.key);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;
let columnPrefs = existing?.forColumn(column.key);

columnPrefs.delete(key);
columnPrefs?.delete(key);

return prefs.persist();
},
Expand All @@ -170,20 +171,20 @@ export const preferences = {
*/
get(key: string) {
let prefs = column.table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let columnPrefs = existing.forColumn(column.key);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;
let columnPrefs = existing?.forColumn(column.key);

return columnPrefs.get(key);
return columnPrefs?.get(key);
},
/**
* set an entry on the underlying `Map` used for this column-plugin pair
*/
set(key: string, value: unknown) {
let prefs = column.table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let columnPrefs = existing.forColumn(column.key);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;
let columnPrefs = existing?.forColumn(column.key);

columnPrefs.set(key, value);
columnPrefs?.set(key, value);

prefs.persist();
},
Expand All @@ -196,7 +197,7 @@ export const preferences = {
* returns an object for bulk updating preferences data
* for all columns (scoped to key and table)
*/
forAllColumns<P extends BasePlugin<any>, Data = unknown>(table: Table<Data>, klass: Class<P>) {
forAllColumns<Data = unknown>(table: Table<Data>, klass: PluginClass<any>) {
return {
/**
* delete an entry on every column in the underlying column `Map` for this table-plugin pair
Expand All @@ -206,10 +207,10 @@ export const preferences = {

for (let column of table.columns) {
let prefs = column.table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let columnPrefs = existing.forColumn(column.key);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;
let columnPrefs = existing?.forColumn(column.key);

columnPrefs.delete(key);
columnPrefs?.delete(key);
}

return tablePrefs.persist();
Expand All @@ -227,16 +228,16 @@ export const preferences = {
* (though, if other plugins can guess how the underlying plugin access
* works, they can access this data, too. No security guaranteed)
*/
forTable<P extends BasePlugin<any>, Data = unknown>(table: Table<Data>, klass: Class<P>) {
forTable<Data = unknown>(table: Table<Data>, klass: PluginClass<any>) {
return {
/**
* delete an entry on the underlying `Map` used for this table-plugin pair
*/
delete(key: string) {
let prefs = table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;

existing.table.delete(key);
existing?.table.delete(key);

return prefs.persist();
},
Expand All @@ -245,18 +246,18 @@ export const preferences = {
*/
get(key: string) {
let prefs = table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;

return existing.table.get(key);
return existing?.table.get(key);
},
/**
* set an entry on the underlying `Map` used for this table-plugin pair
*/
set(key: string, value: unknown) {
let prefs = table.preferences;
let existing = prefs.storage.forPlugin(klass.name);
let existing = klass.pluginName ? prefs.storage.forPlugin(klass.pluginName) : null;

existing.table.set(key, value);
existing?.table.set(key, value);

return prefs.persist();
},
Expand Down Expand Up @@ -297,7 +298,7 @@ function columnsFor<DataType = any>(

if (requester) {
assert(
`[${requester.name}] requested columns from the table, but the plugin, ${requester.name}, ` +
`[${requester.pluginName}] requested columns from the table, but the plugin, ${requester.pluginName}, ` +
`is not used in this table`,
table.plugins.some((plugin) => plugin instanceof (requester as Class<Plugin>))
);
Expand All @@ -313,7 +314,7 @@ function columnsFor<DataType = any>(
if (reordering && reordering.constructor === requester) {
if (visibility) {
assert(
`<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
`<#${visibility.pluginName}> defined a 'columns' property, but did not return valid data.`,
visibility.columns && Array.isArray(visibility.columns)
);

Expand All @@ -325,7 +326,7 @@ function columnsFor<DataType = any>(

if (reordering) {
assert(
`<#${reordering.name}> defined a 'columns' property, but did not return valid data.`,
`<#${reordering.pluginName}> defined a 'columns' property, but did not return valid data.`,
reordering.columns && Array.isArray(reordering.columns)
);

Expand All @@ -334,7 +335,7 @@ function columnsFor<DataType = any>(

if (visibility) {
assert(
`<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
`<#${visibility.pluginName}> defined a 'columns' property, but did not return valid data.`,
visibility.columns && Array.isArray(visibility.columns)
);

Expand All @@ -343,7 +344,7 @@ function columnsFor<DataType = any>(

if (sizing) {
assert(
`<#${sizing.name}> defined a 'columns' property, but did not return valid data.`,
`<#${sizing.pluginName}> defined a 'columns' property, but did not return valid data.`,
sizing.columns && Array.isArray(sizing.columns)
);

Expand All @@ -359,7 +360,7 @@ function columnsFor<DataType = any>(

if (reordering) {
assert(
`<#${reordering.name}> defined a 'columns' property, but did not return valid data.`,
`<#${reordering.pluginName}> defined a 'columns' property, but did not return valid data.`,
reordering.columns && Array.isArray(reordering.columns)
);

Expand All @@ -368,7 +369,7 @@ function columnsFor<DataType = any>(

if (visibility) {
assert(
`<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
`<#${visibility.pluginName}> defined a 'columns' property, but did not return valid data.`,
visibility.columns && Array.isArray(visibility.columns)
);

Expand All @@ -377,7 +378,7 @@ function columnsFor<DataType = any>(

if (sizing) {
assert(
`<#${sizing.name}> defined a 'columns' property, but did not return valid data.`,
`<#${sizing.pluginName}> defined a 'columns' property, but did not return valid data.`,
sizing.columns && Array.isArray(sizing.columns)
);

Expand Down Expand Up @@ -489,16 +490,19 @@ export const meta = {
*/
forColumn<P extends BasePlugin<any>, Data = unknown>(
column: Column<Data>,
klass: Class<P>
klass: PluginClass<any>
): ColumnMetaFor<SignatureFrom<P>> {
let columnMeta = column.table[COLUMN_META_KEY];

return getPluginInstance(columnMeta, column, klass, () => {
let plugin = column.table.pluginOf(klass);

assert(`[${klass.name}] cannot get plugin instance of unregistered plugin class`, plugin);
assert(`<#${plugin.name}> plugin does not have meta specified`, plugin.meta);
assert(`<#${plugin.name}> plugin does not specify column meta`, plugin.meta.column);
assert(
`[${klass.pluginName}] cannot get plugin instance of unregistered plugin class`,
plugin
);
assert(`<#${klass.pluginName}> plugin does not have meta specified`, plugin.meta);
assert(`<#${klass.pluginName}> plugin does not specify column meta`, plugin.meta.column);

return new plugin.meta.column(column);
});
Expand All @@ -514,16 +518,21 @@ export const meta = {
*/
forRow<P extends BasePlugin<any>, Data = unknown>(
row: Row<Data>,
klass: Class<P>
klass: PluginClass<any>
): RowMetaFor<SignatureFrom<P>> {
let rowMeta = row.table[ROW_META_KEY];

return getPluginInstance(rowMeta, row, klass, () => {
let plugin = row.table.pluginOf(klass);

assert(`[${klass.name}] cannot get plugin instance of unregistered plugin class`, plugin);
assert(`<#${plugin.name}> plugin does not have meta specified`, plugin.meta);
assert(`<#${plugin.name}> plugin does not specify row meta`, plugin.meta.row);
assert(
`[${klass.pluginName}] cannot get plugin instance of unregistered plugin class`,
plugin
);

assert(`<#${klass.pluginName}> plugin does not have meta specified`, plugin.meta);

assert(`<#${klass.pluginName}> plugin does not specify row meta`, plugin.meta.row);

return new plugin.meta.row(row);
});
Expand All @@ -537,23 +546,27 @@ export const meta = {
*/
forTable<P extends BasePlugin<any>, Data = unknown>(
table: Table<Data>,
klass: Class<P>
klass: PluginClass<any>
): TableMetaFor<SignatureFrom<P>> {
let tableMeta = table[TABLE_META_KEY];

return getPluginInstance(tableMeta, klass, () => {
let plugin = table.pluginOf(klass);

assert(`[${klass.name}] cannot get plugin instance of unregistered plugin class`, plugin);
assert(`<#${plugin.name}> plugin does not have meta specified`, plugin.meta);
assert(`<#${plugin.name}> plugin does not specify table meta`, plugin.meta.table);
assert(
`<#${plugin.name}> plugin already exists for the table. ` +
`[${klass.pluginName}] cannot get plugin instance of unregistered plugin class`,
plugin
);
assert(`<#${klass.pluginName}> plugin does not have meta specified`, plugin.meta);
assert(`<#${klass.pluginName}> plugin does not specify table meta`, plugin.meta.table);
assert(
`<#${klass.pluginName}> plugin already exists for the table. ` +
`A plugin may only be instantiated once per table.`,

![...tableMeta.keys()].includes(klass)
);

return new plugin.meta.table(table);
return plugin?.meta?.table && new plugin.meta.table(table);
});
},

Expand Down Expand Up @@ -662,7 +675,7 @@ export const options = {
*/
forTable<P extends BasePlugin<any>, Data = unknown>(
table: Table<Data>,
klass: Class<P>
klass: PluginClass<any>
): Partial<OptionsFor<SignatureFrom<P>>> {
let normalized = normalizePluginsConfig(table?.config?.plugins);
let tuple = normalized?.find((option) => option[0] === klass);
Expand All @@ -678,7 +691,7 @@ export const options = {

forColumn<P extends BasePlugin<any>, Data = unknown>(
column: Column<Data>,
klass: Class<P>
klass: PluginClass<any>
): Partial<ColumnOptionsFor<SignatureFrom<P>>> {
let tuple = column.config.pluginOptions?.find((option) => option[0] === klass);
let t = tuple as [unknown, () => ColumnOptionsFor<SignatureFrom<P>>];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface Signature {
}

export class ColumnReordering extends BasePlugin<Signature> {
name = 'column-reordering';
static pluginName = 'column-reordering';
static features = ['columnOrder'];

meta = {
Expand Down
8 changes: 4 additions & 4 deletions ember-headless-table/src/plugins/column-resizing/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface Signature {
* but a plugin can have a "Meta" for each column
*/
export class ColumnResizing extends BasePlugin<Signature> {
name = 'column-resizing';
static pluginName = 'column-resizing';
static features = ['columnWidth'];

meta = {
Expand Down Expand Up @@ -270,7 +270,7 @@ export class TableMeta {
}

get isResizable() {
return this.options?.enabled ?? true;
return this.options?.['enabled'] ?? true;
}

get defaultColumnWidth() {
Expand Down Expand Up @@ -307,7 +307,7 @@ export class TableMeta {
let tablePrefs = this.table.preferences;

for (let column of visibleColumnMetas) {
let existing = tablePrefs.storage.forPlugin('ColumnResizing');
let existing = tablePrefs.storage.forPlugin(ColumnResizing.pluginName);
let columnPrefs = existing.forColumn(column.key);

columnPrefs.set('width', column.width.toString());
Expand Down Expand Up @@ -362,7 +362,7 @@ export class TableMeta {
* options
*/
let isDraggingRight = delta > 0;
let position = this.options?.handlePosition ?? 'left';
let position = this.options?.['handlePosition'] ?? 'left';

let growingColumn: Column | null | undefined;

Expand Down
Loading

0 comments on commit 9f3d290

Please sign in to comment.