-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new
@inertia
tag parameters
- Loading branch information
1 parent
d4bcbd9
commit 746a95d
Showing
7 changed files
with
223 additions
and
55 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
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
/* | ||
* @adonisjs/inertia | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { encode } from 'html-entities' | ||
import type { PluginFn } from 'edge.js/types' | ||
|
||
import debug from '../../debug.js' | ||
import { inertiaHeadTag, inertiaTag } from './tags.js' | ||
|
||
/** | ||
* Register the Inertia tags and globals within Edge | ||
*/ | ||
export const edgePluginInertia: () => PluginFn<undefined> = () => { | ||
return (edge) => { | ||
debug('sharing globals and inertia tags with edge') | ||
|
||
/** | ||
* Register the `inertia` global used by the `@inertia` tag | ||
*/ | ||
edge.global( | ||
'inertia', | ||
(page: Record<string, unknown> = {}, attributes: Record<string, any> = {}) => { | ||
if (page.ssrBody) return page.ssrBody | ||
|
||
const className = attributes?.class ? ` class="${attributes.class}"` : '' | ||
const id = attributes?.id ? ` id="${attributes.id}"` : ' id="app"' | ||
const tag = attributes?.as || 'div' | ||
const dataPage = encode(JSON.stringify(page)) | ||
|
||
return `<${tag}${id}${className} data-page="${dataPage}"></${tag}>` | ||
} | ||
) | ||
|
||
/** | ||
* Register the `inertiaHead` global used by the `@inertiaHead` tag | ||
*/ | ||
edge.global('inertiaHead', (page: Record<string, unknown>) => { | ||
const { ssrHead = [] }: { ssrHead?: string[] } = page || {} | ||
return ssrHead.join('\n') | ||
}) | ||
|
||
/** | ||
* Register tags | ||
*/ | ||
edge.registerTag(inertiaHeadTag) | ||
edge.registerTag(inertiaTag) | ||
} | ||
} |
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,79 @@ | ||
/* | ||
* @adonisjs/inertia | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { EdgeError } from 'edge-error' | ||
import { TagContract } from 'edge.js/types' | ||
|
||
import { isSubsetOf } from './utils.js' | ||
|
||
/** | ||
* `@inertia` tag is used to generate the root element with | ||
* encoded page data. | ||
* | ||
* We can pass an object with `as`, `class` and `id` properties | ||
* - `as` is the tag name for the root element. Defaults to `div` | ||
* - `class` is the class name for the root element. | ||
* - `id` is the id for the root element. Defaults to `app` | ||
*/ | ||
export const inertiaTag: TagContract = { | ||
block: false, | ||
tagName: 'inertia', | ||
seekable: true, | ||
compile(parser, buffer, { filename, loc, properties }) { | ||
/** | ||
* Handle case where no arguments are passed to the tag | ||
*/ | ||
if (properties.jsArg.trim() === '') { | ||
buffer.writeExpression(`out += state.inertia(state.page)`, filename, loc.start.line) | ||
return | ||
} | ||
|
||
/** | ||
* Get AST for the arguments and ensure it is a valid object expression | ||
*/ | ||
properties.jsArg = `(${properties.jsArg})` | ||
const parsed = parser.utils.transformAst( | ||
parser.utils.generateAST(properties.jsArg, loc, filename), | ||
filename, | ||
parser | ||
) | ||
|
||
isSubsetOf(parsed, ['ObjectExpression'], () => { | ||
const { line, col } = parser.utils.getExpressionLoc(parsed) | ||
|
||
throw new EdgeError( | ||
`"${properties.jsArg}" is not a valid argument for @inertia`, | ||
'E_UNALLOWED_EXPRESSION', | ||
{ line, col, filename } | ||
) | ||
}) | ||
|
||
/** | ||
* Stringify the object expression and pass it to the `inertia` helper | ||
*/ | ||
const attributes = parser.utils.stringify(parsed) | ||
buffer.writeExpression( | ||
`out += state.inertia(state.page, ${attributes})`, | ||
filename, | ||
loc.start.line | ||
) | ||
}, | ||
} | ||
|
||
/** | ||
* `@inertiaHead` tag | ||
*/ | ||
export const inertiaHeadTag: TagContract = { | ||
block: false, | ||
tagName: 'inertiaHead', | ||
seekable: false, | ||
compile(_, buffer, { filename, loc }) { | ||
buffer.writeExpression(`out += state.inertiaHead(state.page)`, filename, loc.start.line) | ||
}, | ||
} |
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,23 @@ | ||
import { expressions as expressionsList } from 'edge-parser' | ||
|
||
type ExpressionList = readonly (keyof typeof expressionsList | 'ObjectPattern' | 'ArrayPattern')[] | ||
|
||
/** | ||
* Validates the expression type to be part of the allowed | ||
* expressions only. | ||
* | ||
* The filename is required to report errors. | ||
* | ||
* ```js | ||
* isNotSubsetOf(expression, ['Literal', 'Identifier'], () => {}) | ||
* ``` | ||
*/ | ||
export function isSubsetOf( | ||
expression: any, | ||
expressions: ExpressionList, | ||
errorCallback: () => void | ||
) { | ||
if (!expressions.includes(expression.type)) { | ||
errorCallback() | ||
} | ||
} |
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