-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add formats option for specifying allowed formats
- Loading branch information
Showing
6 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/quill/src/core/utils/createRegistryWithFormats.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Registry } from 'parchment'; | ||
|
||
const MAX_REGISTER_ITERATIONS = 100; | ||
const CORE_FORMATS = ['block', 'break', 'cursor', 'inline', 'scroll', 'text']; | ||
|
||
const createRegistryWithFormats = ( | ||
formats: string[], | ||
sourceRegistry: Registry, | ||
logError: (errorMessage: string) => void, | ||
) => { | ||
const registry = new Registry(); | ||
CORE_FORMATS.forEach((name) => { | ||
const coreBlot = sourceRegistry.query(name); | ||
if (coreBlot) registry.register(coreBlot); | ||
}); | ||
|
||
formats.forEach((name) => { | ||
let format = sourceRegistry.query(name); | ||
if (!format) { | ||
logError( | ||
`Cannot register "${name}" specified in "formats" config. Are you sure it was registered?`, | ||
); | ||
} | ||
let iterations = 0; | ||
while (format) { | ||
registry.register(format); | ||
format = 'blotName' in format ? format.requiredContainer ?? null : null; | ||
|
||
iterations += 1; | ||
if (iterations > MAX_REGISTER_ITERATIONS) { | ||
logError(`Maximum iterations reached when registering "${name}"`); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
return registry; | ||
}; | ||
|
||
export default createRegistryWithFormats; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/quill/test/unit/core/utils/createRegistryWithFormats.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import '../../../../src/quill.js'; | ||
import { describe, expect, test, vitest } from 'vitest'; | ||
import createRegistryWithFormats from '../../../../src/core/utils/createRegistryWithFormats.js'; | ||
import { globalRegistry } from '../../../../src/core/quill.js'; | ||
|
||
describe('createRegistryWithFormats', () => { | ||
test('register core formats', () => { | ||
const registry = createRegistryWithFormats([], globalRegistry, () => {}); | ||
expect(registry.query('cursor')).toBeTruthy(); | ||
expect(registry.query('bold')).toBeFalsy(); | ||
}); | ||
|
||
test('register specified formats', () => { | ||
const registry = createRegistryWithFormats( | ||
['bold'], | ||
globalRegistry, | ||
() => {}, | ||
); | ||
expect(registry.query('cursor')).toBeTruthy(); | ||
expect(registry.query('bold')).toBeTruthy(); | ||
}); | ||
|
||
test('report missing formats', () => { | ||
const logError = vitest.fn(); | ||
const registry = createRegistryWithFormats( | ||
['my-unknown'], | ||
globalRegistry, | ||
logError, | ||
); | ||
expect(registry.query('my-unknown')).toBeFalsy(); | ||
expect(logError).toHaveBeenCalledWith(expect.stringMatching('my-unknown')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters