Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@markrendle
Created July 2, 2014 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrendle/6f1c7d6faf7d9ffe1501 to your computer and use it in GitHub Desktop.
Save markrendle/6f1c7d6faf7d9ffe1501 to your computer and use it in GitHub Desktop.
sgMarkdown
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