-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(okam-build): add text content auto wrap by text element in quick…
… app template transformation
- Loading branch information
Showing
2 changed files
with
59 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
53 changes: 53 additions & 0 deletions
53
packages/okam-build/lib/processor/template/transform/quick/text.js
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,53 @@ | ||
/** | ||
* @file Wrapping the text content using text element, as for in quick app only | ||
* `text`, `a`, `span`, `label` component can place the text content. | ||
* e.g., <view>Hello</view> output: <view><text>Hello</text></view> | ||
* @author [email protected] | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const TEXT_CONTAINER = ['a', 'text', 'span', 'label']; | ||
|
||
function wrapTextData(parentNode, nodeIdx, textNode) { | ||
let {children} = parentNode; | ||
let newParentNode = { | ||
type: 'tag', | ||
name: 'text', | ||
children: [textNode], | ||
prev: nodeIdx > 0 ? parentNode[nodeIdx - 1] : null, | ||
next: null, | ||
parent: parentNode | ||
}; | ||
textNode.parent = newParentNode; | ||
|
||
if (nodeIdx > 0) { | ||
// up next pointer | ||
children[nodeIdx - 1].next = newParentNode; | ||
} | ||
|
||
children.splice(nodeIdx, 1, newParentNode); | ||
} | ||
|
||
function processElementTextData(element) { | ||
let {name, children} = element; | ||
if (TEXT_CONTAINER.includes(name)) { | ||
return; | ||
} | ||
|
||
let textNodes = []; | ||
children.forEach((node, index) => { | ||
let {type, data} = node; | ||
if (type === 'text' && data.trim().length) { | ||
textNodes.push({index, node}); | ||
} | ||
}); | ||
|
||
if (!textNodes.length) { | ||
return; | ||
} | ||
|
||
textNodes.forEach(({index, node}) => wrapTextData(element, index, node)); | ||
} | ||
|
||
module.exports = processElementTextData; |