-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests passing for refactored plugin API and blockquote implemented us…
…ing new API
- Loading branch information
1 parent
068c130
commit 06fbda0
Showing
12 changed files
with
203 additions
and
172 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 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 |
---|---|---|
@@ -1,69 +1,67 @@ | ||
import { LayoutPlugin, HtmlNode, LayoutContext, CssBox, BoxType } from "../.." | ||
import { LayoutManager } from "../../LayoutManager" | ||
import { CssBoxFactoryFunc } from "./CssBoxFactory" | ||
|
||
class BlockquotePlugin implements LayoutPlugin { | ||
public elementName: string = "blockquote" | ||
|
||
private static insertBlockquotePrefixes( | ||
box: CssBox, | ||
context: LayoutContext, | ||
manager: LayoutManager | ||
): void { | ||
/* insert the blockquote "> " prefix at the beginning of each block box that contains inlines (i.e. does /not/ start a block formatting context). | ||
* This approach handles things like one block box containing another (which without this check would put two prefixes in the same rendered line) by putting the prefix only on a single block that will actually create an inline | ||
*/ | ||
const needChecked: CssBox[] = [] | ||
needChecked.push(box) | ||
const needPrefix: CssBox[] = [] | ||
while (needChecked.length > 0) { | ||
const parent: CssBox = needChecked.pop() | ||
for (const box of parent.children) { | ||
if (parent.doesEstablishBlockFormattingContext) { | ||
// go one level deeper, as his children (or grandchildren) each will create a new line | ||
needChecked.push(box) | ||
} else { | ||
// then this guy and all of his siblings need prefixes; they are the deepest nodes in the tree that are inlines | ||
console.assert( | ||
!parent.doesEstablishBlockFormattingContext, | ||
`Expected the parent '${parent.debugNote}' to be establishing a new block formatting context` | ||
) | ||
//Array.prototype.forEach.call(parent.children, b => needPrefix.push(b)) | ||
needPrefix.push(parent) | ||
break // we only need one prefix per line - don't add another for each child. | ||
} | ||
} | ||
} | ||
needPrefix.forEach(b => { | ||
b.prependChild( | ||
manager.createBox( | ||
context, | ||
BoxType.inline, | ||
"> ", | ||
[], | ||
"blockquote-prefix" | ||
) | ||
) | ||
}) | ||
} | ||
|
||
public layout( | ||
context: LayoutContext, | ||
manager: LayoutManager, | ||
element: HtmlNode | ||
): CssBox | null { | ||
const state = new BlockquoteState(context) | ||
state.beginBlockquote() | ||
const kids = manager.layout(context, element.children) | ||
state.endBlockquote() | ||
return manager.createBox(context, BoxType.block, "", kids) | ||
} | ||
|
||
// for any block box (i.e. those that create a new line), insert blockquote styling: | ||
public transform( | ||
context: LayoutContext, | ||
boxFactory: CssBoxFactoryFunc, | ||
box: CssBox | ||
): CssBox { | ||
if (box.type === BoxType.inline) return box | ||
let finalBox = box | ||
const state = new BlockquoteState(context) | ||
const depth = state.blockquoteNestingDepth | ||
if (depth > 0) { | ||
//console.debug("DEPTH:", depth) | ||
// we're within at least one level of blockquote: | ||
const stylingPrefix = "> ".repeat(depth) | ||
const styleBox: CssBox = boxFactory( | ||
context, | ||
BoxType.block, | ||
stylingPrefix, | ||
[box], | ||
"blockQuoteAnon" | ||
) | ||
finalBox = styleBox | ||
} | ||
return finalBox | ||
const box = manager.createBox( | ||
context, | ||
BoxType.block, | ||
"", | ||
kids, | ||
"blockquote" | ||
) | ||
BlockquotePlugin.insertBlockquotePrefixes(box, context, manager) | ||
return box | ||
} | ||
} | ||
|
||
export default BlockquotePlugin | ||
|
||
class BlockquoteState { | ||
private static readonly BlockquoteNestingDepthKey: string = | ||
"blockquote-nesting-depth-key" | ||
|
||
public constructor(readonly context: LayoutContext) {} | ||
|
||
public endBlockquote(): void { | ||
this.context.popState(BlockquoteState.BlockquoteNestingDepthKey) | ||
} | ||
|
||
public beginBlockquote(): void { | ||
this.context.pushState(BlockquoteState.BlockquoteNestingDepthKey, 0) | ||
} | ||
|
||
public get blockquoteNestingDepth(): number { | ||
const stack = this.context.getStateStack( | ||
BlockquoteState.BlockquoteNestingDepthKey | ||
) | ||
return stack.length | ||
} | ||
} |
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 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
Oops, something went wrong.