Skip to content

Commit

Permalink
feat: add property to context
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 28, 2020
1 parent 1d6aa75 commit ae5dd79
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/views/alert.edge
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{ inspect($state, 1) }}
@if($slots.title)
{{{ $slots.title({ title }) }}}
@else
Expand Down
3 changes: 2 additions & 1 deletion examples/views/partial.edge
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<h1>Inside slot: {{ slotTitle() }}</h1>
{{ inspect($state, 1) }}
<h1>Inside slot: {{ slotTitle() }}</h1>
15 changes: 13 additions & 2 deletions examples/views/user.edge
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.__inspect_output {
margin-bottom: 20px;
}
</style>
</head>
<body>
@set('user', { username: 'virk' })

{{ inspect($state, 1) }}

@component('alert', { title: 'hello world' })
@slot('title', props)
@include('partial')
@endslot
@endcomponent
</body>
</html>
2 changes: 1 addition & 1 deletion examples/views/user.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ module.exports = class User {
slotTitle (ctx) {
return ctx.resolve('props').title.toUpperCase()
}
}
}
8 changes: 3 additions & 5 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,9 @@ export class Compiler implements CompilerContract {
const wrapAsFunction = !inline

/**
* Get a new instance of the parser. We use the `templatePath` as the filename
* instead of the `absPath`, since `templatePath` is relative and readable.
* Get a new instance of the parser.
*/
const parser = new Parser(this._tags, {
filename: `${absPath.replace(/\.edge$/, '')}.edge`,
})
const parser = new Parser(this._tags, { filename: absPath })

/**
* Resolve the template and Presenter using the given loader
Expand All @@ -252,6 +249,7 @@ export class Compiler implements CompilerContract {
* Finally process the ast
*/
const buffer = new EdgeBuffer()
buffer.writeStatement(`ctx.set('$filename', '${templatePath.replace(/\.edge$/, '')}.edge')`)
templateTokens.forEach((token) => parser.processLexerToken(token, buffer))

const payload = {
Expand Down
17 changes: 15 additions & 2 deletions src/Edge/globals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,24 @@ import { EdgeContract, ContextContract } from '../../Contracts'
* Inspect value.
*/
function inspect (ctx: ContextContract, valueToInspect: any, depth: number = 1) {
return ctx.safe(`<pre style="background: #000; color: #fff; padding: 20px;">${utilInspect(valueToInspect, {
const inspectedString = `<pre>${utilInspect(valueToInspect, {
showHidden: true,
compact: false,
depth: depth,
})}</pre>`)
})}</pre>`

const filename = `<span style="color: #999; position: absolute; right: 20px; top: 10px;">
${ctx.resolve('$filename')}
</span>`

return ctx.safe(`<div class="__inspect_output" style="background: #000; color: #fff; padding: 20px; position: relative;">${inspectedString}${filename}</div>`)
}

/**
* Compacting the inspect output of self
*/
inspect[Symbol.for('nodejs.util.inspect.custom')] = function customInspect () {
return '[inspect]'
}

export default function globals (edge: EdgeContract) {
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const componentNameAllowedExpressions: (keyof typeof expressions)[] = [
* Returns the component name and props by parsing the component jsArg expression
*/
function getComponentNameAndProps (expression: any, parser: Parser): [string, string] {
let name
let name: string

/**
* Use the first expression inside the sequence expression as the name
Expand Down

0 comments on commit ae5dd79

Please sign in to comment.