Created
July 2, 2014 08:11
-
-
Save markrendle/6f1c7d6faf7d9ffe1501 to your computer and use it in GitHub Desktop.
sgMarkdown
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
module SgDirectives { | |
declare function marked(source: string): string; | |
function syncHeights(source: HTMLElement, target: HTMLElement) { | |
var syncHeight = () => target.style.height = source.offsetHeight + "px"; | |
syncHeight(); | |
source.addEventListener("mouseup", syncHeight); | |
} | |
function link(scope: ng.IScope, jelem: ng.IAugmentedJQuery) { | |
var elem = <HTMLElement>jelem[0]; | |
var textarea = <HTMLElement>elem.querySelector("textarea"); | |
var preview = <HTMLElement>elem.querySelector("div.sg-markdown-preview"); | |
if (textarea && preview) { | |
syncHeights(textarea, preview); | |
var ngModel = textarea.getAttribute("ng-model"); | |
scope.$watch(ngModel, (newValue: string) => { | |
preview.innerHTML = newValue ? marked(newValue) : ""; | |
}); | |
} | |
} | |
export function markdown(): ng.IDirective { | |
return { | |
restrict: "E", | |
templateUrl: "/html/directives/markdown.html", | |
link: link, | |
transclude: true | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment