-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Typescriptify attributable + MarkupSection 🐝 * Atom + AtomNode 🐥 * CardNode + Markup 🦏 * Image + ListItem + ListSection 🦀🦀 * LifecycleCallbacks + PostNodeBuilder + fixy fix 🐷 * POST 🦋 * RenderNode + Fixy 🐠
- Loading branch information
Showing
33 changed files
with
656 additions
and
570 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
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,49 @@ | ||
import { entries } from '../utils/object-utils' | ||
import { contains } from '../utils/array-utils' | ||
|
||
export const VALID_ATTRIBUTES = ['data-md-text-align'] | ||
|
||
export interface Attributable { | ||
attributes: { [key: string]: string } | ||
hasAttribute: (key: string) => boolean | ||
setAttribute: (key: string, value: string) => void | ||
removeAttribute: (key: string) => void | ||
getAttribute: (key: string) => string | ||
eachAttribute: (cb: (key: string, value: string) => void) => void | ||
} | ||
|
||
type AbstractConstructor<T> = Function & { prototype: T } | ||
type Constructor<T> = new (...args: any[]) => T | ||
|
||
/* | ||
* A "mixin" to add section attribute support | ||
* to markup and list sections. | ||
*/ | ||
export function attributable<T extends unknown>(Base: AbstractConstructor<T>): Constructor<T & Attributable> { | ||
return class extends (Base as any) { | ||
attributes: { [key: string]: string } = {} | ||
|
||
hasAttribute(key: string) { | ||
return key in this.attributes | ||
} | ||
|
||
setAttribute(key: string, value: string) { | ||
if (!contains(VALID_ATTRIBUTES, key)) { | ||
throw new Error(`Invalid attribute "${key}" was passed. Constrain attributes to the spec-compliant whitelist.`) | ||
} | ||
this.attributes[key] = value | ||
} | ||
|
||
removeAttribute(key: string) { | ||
delete this.attributes[key] | ||
} | ||
|
||
getAttribute(key: string) { | ||
return this.attributes[key] | ||
} | ||
|
||
eachAttribute(cb: (key: string | number, value: string) => void) { | ||
entries(this.attributes).forEach(([k, v]) => cb(k, v)) | ||
} | ||
} as Constructor<T & Attributable> | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { normalizeTagName } from '../utils/dom-utils' | ||
import assert from '../utils/assert' | ||
import Section from './_section' | ||
|
||
type Constructor<T = {}> = new (...args: any[]) => T | ||
|
||
export interface TagNameable { | ||
tagName: string | ||
isValidTagName(normalizedTagName: string): boolean | ||
} | ||
|
||
export function tagNameable(Base: Constructor<Section>) { | ||
abstract class TagNameable extends Base { | ||
_tagName: string | null = null | ||
|
||
set tagName(val: string) { | ||
let normalizedTagName = normalizeTagName(val) | ||
assert(`Cannot set section tagName to ${val}`, this.isValidTagName(normalizedTagName)) | ||
this._tagName = normalizedTagName | ||
} | ||
|
||
get tagName() { | ||
return this._tagName as string | ||
} | ||
|
||
abstract isValidTagName(normalizedTagName: string): boolean | ||
} | ||
|
||
return TagNameable | ||
} |
Oops, something went wrong.