-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new DOM feature ForceElementSizeFeature
This feature will force a specific element to a peculiar size.
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import script from './ForceElementSizeFeature.webjs'; | ||
import { FeatureBuilder } from '../FeatureBuilder'; | ||
import type { DOMElementRequest } from '../types'; | ||
import type { FeatureConstructor } from '../Feature'; | ||
|
||
/** | ||
* An object describing customization for the force body feature. | ||
* | ||
* @public | ||
*/ | ||
export interface ForceElementSizeOptions { | ||
/** | ||
* The element to target. | ||
*/ | ||
target: DOMElementRequest; | ||
/** | ||
* The width to override. | ||
* | ||
* @defaultvalue 'auto' | ||
*/ | ||
widthValue?: number | string; | ||
/** | ||
* The height to override. | ||
* | ||
* @defaultvalue 'auto' | ||
*/ | ||
heightValue?: number | string; | ||
/** | ||
* Force body width to `widthValue`. | ||
* | ||
* @defaultvalue true | ||
*/ | ||
forceWidth?: boolean; | ||
/** | ||
* Force body width to `heightValue`. | ||
* | ||
* @defaultvalue true | ||
*/ | ||
forceHeight?: boolean; | ||
/** | ||
* When no element is found matching the target, should the script raise an | ||
* error? | ||
* | ||
* @defaultValue false | ||
*/ | ||
shouldThrowWhenNotFound?: boolean; | ||
} | ||
|
||
const defaultOptions: ForceElementSizeOptions = { | ||
forceHeight: true, | ||
forceWidth: true, | ||
widthValue: 'auto', | ||
heightValue: 'auto', | ||
shouldThrowWhenNotFound: false | ||
} as ForceElementSizeOptions; | ||
|
||
/** | ||
* This feature sets element size programmatically and only once, when | ||
* {@link https://developer.mozilla.org/fr/docs/Web/Events/DOMContentLoaded | DOMContentLoaded} | ||
* has been fired. | ||
* | ||
* @public | ||
*/ | ||
export const ForceElementSizeFeature: FeatureConstructor<ForceElementSizeOptions> = new FeatureBuilder< | ||
ForceElementSizeOptions | ||
>({ | ||
script, | ||
defaultOptions, | ||
featureIdentifier: 'org.formidable-webview/webshell.force-element-size', | ||
className: 'ForceElementSizeFeature' | ||
}).build(); |
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,25 @@ | ||
function ForceElementSizeFeature(context) { | ||
var options = context.options || {}; | ||
var widthValue = options.widthValue || 'auto'; | ||
var heightValue = options.heightValue || 'auto'; | ||
var forceWidth = options.forceWidth !== false; | ||
var forceHeight = options.forceHeight !== false; | ||
var target = options.target; | ||
var shouldThrowWhenNotFound = options.shouldThrowWhenNotFound === true; | ||
if (!target || (typeof target !== 'string' && typeof target !== 'object')) { | ||
throw new Error('Missing or wrong type for required target option'); | ||
} | ||
var element = context.getDOMSelection(target, false); | ||
if (element) { | ||
if (forceWidth) { | ||
element.style.width = widthValue; | ||
} | ||
if (forceHeight) { | ||
element.style.height = heightValue; | ||
} | ||
} else if (shouldThrowWhenNotFound) { | ||
throw new Error( | ||
"Couldn't find an element for target " + JSON.stringify(target) | ||
); | ||
} | ||
} |