-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support soft wrap for line numbers plugin #584
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53754cb
Added dsstore to gitignore
VitaliyR e05043f
Added support of soft wrap lines for line numbers plugin
VitaliyR 8e0ebd5
Removed obsolette function
VitaliyR 8ebc48c
Moved lineHeight conditional inside forEach block
VitaliyR de28992
Replaced innerText with textContent for supporting ff
VitaliyR ffae34c
Removed class name for soft wrap, made to determinate it automaticall…
VitaliyR 407fc67
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
VitaliyR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
hide-*.js | ||
node_modules | ||
.idea/ | ||
.idea/ | ||
.DS_Store |
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,38 +1,85 @@ | ||
Prism.hooks.add('after-highlight', function (env) { | ||
// works only for <code> wrapped inside <pre> (not inline) | ||
var pre = env.element.parentNode; | ||
var clsReg = /\s*\bline-numbers\b\s*/; | ||
if ( | ||
!pre || !/pre/i.test(pre.nodeName) || | ||
// Abort only if nor the <pre> nor the <code> have the class | ||
(!clsReg.test(pre.className) && !clsReg.test(env.element.className)) | ||
) { | ||
return; | ||
} | ||
|
||
if (clsReg.test(env.element.className)) { | ||
// Remove the class "line-numbers" from the <code> | ||
env.element.className = env.element.className.replace(clsReg, ''); | ||
} | ||
if (!clsReg.test(pre.className)) { | ||
// Add the class "line-numbers" to the <pre> | ||
pre.className += ' line-numbers'; | ||
} | ||
|
||
var linesNum = (1 + env.code.split('\n').length); | ||
var lineNumbersWrapper; | ||
|
||
var lines = new Array(linesNum); | ||
lines = lines.join('<span></span>'); | ||
|
||
lineNumbersWrapper = document.createElement('span'); | ||
lineNumbersWrapper.className = 'line-numbers-rows'; | ||
lineNumbersWrapper.innerHTML = lines; | ||
|
||
if (pre.hasAttribute('data-start')) { | ||
pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1); | ||
} | ||
|
||
env.element.appendChild(lineNumbersWrapper); | ||
|
||
}); | ||
(function(){ | ||
|
||
/** | ||
* Class name which is used to flag this code field has break-word css attr | ||
* @type {String} | ||
*/ | ||
var BREAK_WORD_CLASS = 'line-numbers-break-word'; | ||
|
||
/** | ||
* Resizes line numbers spans according to height of line of code | ||
* @param {Element} element <code> element | ||
*/ | ||
var _resizeElement = function(element){ | ||
var lineNumbersWrapper = element.querySelector('.line-numbers-rows'); | ||
var lineNumberSizer = element.querySelector('.line-numbers-sizer'); | ||
var codeLines = element.textContent.split('\n'); | ||
var lineHeight; | ||
|
||
lineNumberSizer.style.display = 'block'; | ||
|
||
// parse line height in such way because of problems with zoom & line height css attr | ||
lineNumberSizer.innerText = '\n'; | ||
lineHeight = lineNumberSizer.getBoundingClientRect().height; | ||
|
||
codeLines.forEach(function(line, lineNumber){ | ||
lineNumberSizer.innerText = line; | ||
var lineSize = lineNumberSizer.getBoundingClientRect().height || lineHeight; | ||
lineNumbersWrapper.children[lineNumber].style.height = lineSize + 'px'; | ||
}); | ||
|
||
lineNumberSizer.innerText = ''; | ||
lineNumberSizer.style.display = 'none'; | ||
}; | ||
|
||
window.addEventListener('resize', function(){ | ||
Array.prototype.forEach.call(document.querySelectorAll('pre.' + BREAK_WORD_CLASS), _resizeElement); | ||
}); | ||
|
||
Prism.hooks.add('after-highlight', function (env) { | ||
// works only for <code> wrapped inside <pre> (not inline) | ||
var pre = env.element.parentNode; | ||
var clsReg = /\s*\bline-numbers\b\s*/; | ||
if ( | ||
!pre || !/pre/i.test(pre.nodeName) || | ||
// Abort only if nor the <pre> nor the <code> have the class | ||
(!clsReg.test(pre.className) && !clsReg.test(env.element.className)) | ||
) { | ||
return; | ||
} | ||
|
||
if (clsReg.test(env.element.className)) { | ||
// Remove the class "line-numbers" from the <code> | ||
env.element.className = env.element.className.replace(clsReg, ''); | ||
} | ||
if (!clsReg.test(pre.className)) { | ||
// Add the class "line-numbers" to the <pre> | ||
pre.className += ' line-numbers'; | ||
} | ||
|
||
var linesNum = (1 + env.code.split('\n').length); | ||
var lineNumbersWrapper = document.createElement('span'); | ||
lineNumbersWrapper.className = 'line-numbers-rows'; | ||
|
||
var lines = new Array(linesNum); | ||
lines = lines.join('<span></span>'); | ||
|
||
lineNumbersWrapper.innerHTML = lines; | ||
|
||
if (pre.hasAttribute('data-start')) { | ||
pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1); | ||
} | ||
|
||
env.element.appendChild(lineNumbersWrapper); | ||
|
||
if (pre.classList.contains(BREAK_WORD_CLASS)){ | ||
var lineHeightSizer = document.createElement('span'); | ||
lineHeightSizer.className = 'line-numbers-sizer'; | ||
|
||
env.element.appendChild(lineHeightSizer); | ||
_resizeElement(env.element); | ||
} | ||
|
||
}); | ||
|
||
})(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the fallback? In which case would we get there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are about line 21-23:
Empty line? Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, probably it was legacy from old scenario, and can be deleted.
in forEach block empty line height calculating with same scenario.
I can update the pull request with removing this lines (20-23).
Everything else?